\documentclass[letter,10pt, openany]{book}


\usepackage{amsmath,etex, pseudocode,   fancyvrb, }
\usepackage[usenames]{color}

%%%%%                   Postscript drawing packages
\usepackage{psboxit}
\usepackage{pstricks}
\usepackage{pstricks-add, pst-eucl, here}
\usepackage{fancybox}





%%%%%%%%%

\usepackage{hangcaption}
\usepackage[amsmath,thmmarks,standard,thref]{ntheorem}
\usepackage[colorlinks=true,linkcolor=black, pdftitle={Maple, Maple Algorithms, Algorithms, Discrete Math, Graph Theory, Recursion, Maple}, pdfauthor={david santos}, bookmarksopen, dvips]{hyperref}
\usepackage{pdflscape}
\usepackage{thumbpdf}

%%%%%                  FONTS
%\usepackage[latin1]{inputenc}
% Fourier for math | Utopia (scaled) for rm | Helvetica for ss | Latin Modern for tt
\usepackage{fourier} % math & rm
\usepackage{bookman}
%\usepackage{luximono}
%\renewcommand{\ttdefault}{lmtt} %tt
\usepackage{marvosym}
\usepackage{pifont}
\usepackage{mathrsfs}
\usepackage{stmaryrd}
\usepackage{amssymb}
\usepackage[tiling]{pst-fill}      % PSTricks package for filling/tiling
\usepackage{pst-text}              % PSTricks package for character path
\usepackage{textfit}
\usepackage{pst-grad}              % PSTricks package for gradient filling
\usepackage{multicol}
\usepackage{pseudocode, maplestd2e}
\newcommand{\TM}{\Pisymbol{psy}{228}\ }

\usepackage{answers}
\Newassociation{answerexer}{Answer}{discmatII}
\usepackage{makeidx}
\usepackage{sqrcaps}
\usepackage{fancyhdr} %%%%for page headers and footers
\fontencoding{T1}
    \fontfamily{fmv}
    \selectfont
\DeclareFixedFont{\bigghv}{T1}{phv}{b}{n}{1.2cm}
\DeclareFixedFont{\bighv}{T1}{phv}{b}{n}{0.6cm}
\DeclareFixedFont{\bigithv}{T1}{phv}{b}{sl}{0.8cm}
\DeclareFixedFont{\smallit}{T1}{phv}{b}{it}{9pt}




%%%%%Chapter Heads
\makeatletter
\def\vertiline{\psline[linewidth=2pt,linecolor=magenta](0,2)(0,-1)\hspace{0.1cm}\relax}
\def\vertilines{%
  \let\LV\vertiline\LV\LV\LV\LV\LV\LV\LV\LV\LV\LV}
\def\GrosCarreAvecUnChiffre#1{%
  \psframebox[fillstyle=gradient,gradbegin=yellow,gradend=magenta]{%
 \hspace{2mm} \red #1 \hspace{2mm}}
}

\def\@makechapterhead#1{\hbox{%
    \huge
    \vertilines
    \hspace{-0.5cm}%
    \GrosCarreAvecUnChiffre{\thechapter}
    \hspace{0.2cm}\hbox{#1}%
}\par\vskip 2cm}
\def\@makeschapterhead#1{\hbox{%
    \huge
    \vertilines
    %\hspace{0.5cm}%
    \hbox{#1}%
}\par\vskip 2cm}
 \makeatother
\makeatletter
\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\blue\bighv}}
\makeatother

 \mathversion{bold}

\newcommand{\WriteBig}[5] {%
  \DeclareFixedFont{\bigsf}{T1}{phv}{b}{n}{#2}%
  \DeclareFixedFont{\smallrm}{T1}{ptm}{(m)}{n}{#3}%
  \psboxfill{\smallrm  #1} %
      \centerline{%
         \pscharpath[fillstyle=gradient,%
        gradangle=-45,%
        gradmidpoint=0.5,%
        addfillstyle=boxfill,%
        gradbegin=#4,%
        gradend=#5,%
        fillangle=45,%
        fillsep=0.7mm]{\rput[b](0,0){\bigsf#1}}}
}




%%%%%%%%%MACRO FOR LONG DIVISION
\newdimen\digitwidth
\settowidth\digitwidth{0}
\def~{\hspace{\digitwidth}}
\def\divrule#1#2{%
\noalign{\moveright#1\digitwidth%
\vbox{\hrule width#2\digitwidth}}}
%%%%%%%%%%%%%%


%%%%%FLOAT PLACEMENT
\renewcommand{\textfraction}{0.05}
\renewcommand{\topfraction}{0.95}
\renewcommand{\bottomfraction}{0.95}
\renewcommand{\floatpagefraction}{0.35}
\setcounter{totalnumber}{5}

%%%%%%%%%PRINTED AREA
%\topmargin -.6in \textheight 9in \oddsidemargin -.3in
%\evensidemargin -.3in \textwidth 7in \setlength{\fboxsep}{.15in}
%\setlength{\fboxrule}{1pt}
\usepackage[margin=2cm]{geometry}


%%%%%                    THEOREM-LIKE ENVIRONMENTS
\newcommand{\proofsymbol}{\Pisymbol{pzd}{113}}


\theorempreskipamount .5cm \theorempostskipamount .5cm
\theoremstyle{change} \theoremheaderfont{\sffamily\bfseries}
\theorembodyfont{\normalfont}
\newtheorem{thm}{Theorem}
\newtheorem{exer}[thm]{Exercise}
 \newtheorem{cor}[thm]{Corollary}
\newtheorem{df}[thm]{Definition}
\newtheorem{axi}[thm]{Axiom}
\newtheorem{exa}[thm]{Example}
\newtheorem{lem}[thm]{Lemma}
\newenvironment{pf}[0]{\itshape\begin{quote}{\bf Proof: \ }}{\proofsymbol\end{quote}}



\newenvironment{solu}[0]{\begin{quote}{\bf Solution:  \ }$\blacktriangleright$ \itshape }{$\blacktriangleleft$\end{quote}}
\newenvironment{rem}[0]{\begin{quote}{\Huge\textcolor{red}{\Pisymbol{pzd}{43}}}\small\itshape }{\end{quote}}
\newcommand{\QUOTEME}[2]{\begin{quote}{\it\textbf{#1}}\nolinebreak[1] \blue\hspace*{\fill} \mbox{-\textsl{#2}} \hspace*{\fill}\end{quote}}



%%%%%                Non-standard commands and symbols
\newcommand{\BBZ}{\mathbb{Z}}
\newcommand{\BBR}{\mathbb{R}}
\newcommand{\BBN}{\mathbb{N}}
\newcommand{\BBC}{\mathbb{C}}
\newcommand{\BBQ}{\mathbb{Q}}
\newcommand{\dsum}{\displaystyle\sum}
\def\lcm#1{\mathrm{lcm}\left(#1\right)}

\def\binom#1#2{\displaystyle{#1\choose#2}}
\def\arc#1{{\wideparen{#1}}}
\def\opp#1{{\bf Opp}\left(#1\right)}
\def\absval#1{\left|#1\right|}
\newcommand{\dis}{\displaystyle}
\newcommand{\bcp}{\begin{center}\begin{pseudocode}}
\newcommand{\ecp}{\end{pseudocode}\end{center}}
\def\binom#1#2{{#1\choose#2}}
\def\arc#1{{\wideparen{#1}}}
\def\fun#1#2#3#4#5{\everymath{\displaystyle}{{#1} : \vspace{1cm}
\begin{array}{ccc}{#4} & \rightarrow &
{#5}\\
{#2} &  \mapsto & {#3} \\
\end{array}}}
\def\sgn#1{{\mathrm{signum}}\left(#1\right)}
\newcommand{\comp}{\complement}
\def\dom#1{{\mathbf{Dom}}\left(#1\right)}
\def\target#1{{\mathbf{Target}}\left(#1\right)}
\def\card#1{\mathrm{card}\left(#1\right)}
\def\im#1{{\mathbf{Im}}\left(#1\right)}
\def\floor#1{\llfloor #1 \rrfloor}
\def\ceil#1{\llceil #1 \rrceil}
\def\d#1{\mathrm{d}#1}
\newcommand{\dint}{\displaystyle \int}
\def\seq#1#2#3{\left\{#1\right\} _{#2} ^{#3}}
\newcommand{\ngrows}{n\rightarrow +\infty}
\newcommand{\ngroes}{n\rightarrow +\infty}
\newcommand{\sequ}{\left\{a_n\right\} _{n=1} ^{+\infty}}

\def\soo#1{\mathit{o}\left(#1\right)}
\def\smallo#1#2{\mathit{o}_{\substack #1}\left(#2\right)}
\def\asympto#1{\sim_{\substack #1}}
\def\bigo#1#2{{\cal O}_{\substack #1}\left(#2\right)}
\def\ll#1#2{<<_{\substack #1}\left(#2\right)}
\def\boo#1{{\cal O}\left(#1\right)}
%\def\maplecode#1{{\tt #1}}
%%%%%%%%%%%%%%%%%INTERVALS
%%%%%%%% lo= left open, rc = right closed, etc.
\def\lcrc#1#2{{\bf\Big[ }#1 \ ; #2 {\bf\Big] }}
\def\loro#1#2{{\bf\Big] }#1 \ ; #2 {\bf \Big[}}
\def\lcro#1#2{{\bf \Big[} #1 \ ; #2 {\bf \Big[} }
\def\lorc#1#2{\Big]#1 \ ; #2 \Big]}
\def\seg#1{\left[ #1\right]}
\def\ray#1{\left[ #1\right[}
\def\ang{\widehat}


%%%%Title Page

\makeatletter
\def\La{%
  L\kern-.36em{%
  \setbox0\hbox{T}%
  \vbox to\ht0{%
    \hbox{$\m@th$%
      \csname S@\f@size\endcsname
      \fontsize\sf@size\z@
      \math@fontsfalse\selectfont A}%
      \vss%
    }%
  }%
} \makeatother


%%%%%%%





%%%%%%%%%

%%%%%%%%%%%%%%



\renewcommand{\arraystretch}{2.3}


\everymath{\displaystyle}





\makeatletter
\newenvironment{tablehere}
  {\def\@captype{table}}
  {}

\newenvironment{figurehere}
  {\def\@captype{figure}}
  {}
\makeatother

%%%%%%%%%%%%%%%%%%
%%%%%%                  And voilà the document !!!

\begin{document}
\begin{frontmatter}
\pagenumbering{roman}
\begin{titlepage}
\psset{unit=1in,linewidth=2pt} \psline(-0.75,2)(-0.75,-\paperheight)
\psline(-0.25,2)(-0.25,-\paperheight)

\vspace*{3cm} \centerline{\Huge\textsqrc{More Discrete Mathematics}}

\vspace*{2cm} \centerline{\Large \textcolor{red}{David A. SANTOS}
\\ \href{mailto:dsantos@ccp.edu}{dsantos@ccp.edu}}
\vfill
\end{titlepage}
\chapter*{Notice}

\section*{Legal Notice}


\begin{quote}
    Copyright \copyright{}  2007  David Anthony SANTOS.
    Permission is granted to copy, distribute and/or modify this document
    under the terms of the GNU Free Documentation License, Version 1.2
    or any later version published by the Free Software Foundation;
    with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
    A copy of the license is included in the section entitled ``GNU
    Free Documentation License''.
\end{quote}


\section*{Updates} The most current version of this book (both the
.pdf and the .tex source) can be found here:

\centerline{\url{faculty.ccp.edu/faculty/dsantos/lecture_notes.html}}





\clearpage










\lhead[\rm\thepage]{\it \rightmark} \rhead[\it \leftmark]{}
\lfoot{\textcolor{darkgray}{Free to photocopy and
distribute}}\cfoot{}
\renewcommand{\sectionmark}{\markboth{\sectionname\ \thechapter}}
\renewcommand{\sectionmark}{\markright}

\tableofcontents


\clearpage







{\tiny\chapter*{\rlap{GNU Free Documentation License}}
\phantomsection  % so hyperref creates bookmarks
\addcontentsline{toc}{chapter}{GNU Free Documentation License}
%\label{label_fdl}

 \begin{center}

       Version 1.2, November 2002


 Copyright \copyright{} 2000,2001,2002  Free Software Foundation, Inc.

 \bigskip

     51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 \bigskip

 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.
\end{center}


\begin{center}
{\bf\large Preamble}
\end{center}

The purpose of this License is to make a manual, textbook, or other
functional and useful document ``free'' in the sense of freedom: to
assure everyone the effective freedom to copy and redistribute it,
with or without modifying it, either commercially or
noncommercially. Secondarily, this License preserves for the author
and publisher a way to get credit for their work, while not being
considered responsible for modifications made by others.

This License is a kind of ``copyleft'', which means that derivative
works of the document must themselves be free in the same sense.  It
complements the GNU General Public License, which is a copyleft
license designed for free software.

We have designed this License in order to use it for manuals for
free software, because free software needs free documentation: a
free program should come with manuals providing the same freedoms
that the software does.  But this License is not limited to software
manuals; it can be used for any textual work, regardless of subject
matter or whether it is published as a printed book.  We recommend
this License principally for works whose purpose is instruction or
reference.


\begin{center}
{\Large\bf 1. APPLICABILITY AND DEFINITIONS\par} \phantomsection
\addcontentsline{toc}{section}{1. APPLICABILITY AND DEFINITIONS}
\end{center}

This License applies to any manual or other work, in any medium,
that contains a notice placed by the copyright holder saying it can
be distributed under the terms of this License.  Such a notice
grants a world-wide, royalty-free license, unlimited in duration, to
use that work under the conditions stated herein.  The
``\textbf{Document}'', below, refers to any such manual or work. Any
member of the public is a licensee, and is addressed as
``\textbf{you}''.  You accept the license if you copy, modify or
distribute the work in a way requiring permission under copyright
law.

A ``\textbf{Modified Version}'' of the Document means any work
containing the Document or a portion of it, either copied verbatim,
or with modifications and/or translated into another language.

A ``\textbf{Secondary Section}'' is a named appendix or a
front-matter section of the Document that deals exclusively with the
relationship of the publishers or authors of the Document to the
Document's overall subject (or to related matters) and contains
nothing that could fall directly within that overall subject. (Thus,
if the Document is in part a textbook of mathematics, a Secondary
Section may not explain any mathematics.)  The relationship could be
a matter of historical connection with the subject or with related
matters, or of legal, commercial, philosophical, ethical or
political position regarding them.

The ``\textbf{Invariant Sections}'' are certain Secondary Sections
whose titles are designated, as being those of Invariant Sections,
in the notice that says that the Document is released under this
License.  If a section does not fit the above definition of
Secondary then it is not allowed to be designated as Invariant.  The
Document may contain zero Invariant Sections.  If the Document does
not identify any Invariant Sections then there are none.

The ``\textbf{Cover Texts}'' are certain short passages of text that
are listed, as Front-Cover Texts or Back-Cover Texts, in the notice
that says that the Document is released under this License.  A
Front-Cover Text may be at most 5 words, and a Back-Cover Text may
be at most 25 words.

A ``\textbf{Transparent}'' copy of the Document means a
machine-readable copy, represented in a format whose specification
is available to the general public, that is suitable for revising
the document straightforwardly with generic text editors or (for
images composed of pixels) generic paint programs or (for drawings)
some widely available drawing editor, and that is suitable for input
to text formatters or for automatic translation to a variety of
formats suitable for input to text formatters.  A copy made in an
otherwise Transparent file format whose markup, or absence of
markup, has been arranged to thwart or discourage subsequent
modification by readers is not Transparent. An image format is not
Transparent if used for any substantial amount of text.  A copy that
is not ``Transparent'' is called ``\textbf{Opaque}''.

Examples of suitable formats for Transparent copies include plain
ASCII without markup, Texinfo input format, LaTeX input format, SGML
or XML using a publicly available DTD, and standard-conforming
simple HTML, PostScript or PDF designed for human modification.
Examples of transparent image formats include PNG, XCF and JPG.
Opaque formats include proprietary formats that can be read and
edited only by proprietary word processors, SGML or XML for which
the DTD and/or processing tools are not generally available, and the
machine-generated HTML, PostScript or PDF produced by some word
processors for output purposes only.

The ``\textbf{Title Page}'' means, for a printed book, the title
page itself, plus such following pages as are needed to hold,
legibly, the material this License requires to appear in the title
page.  For works in formats which do not have any title page as
such, ``Title Page'' means the text near the most prominent
appearance of the work's title, preceding the beginning of the body
of the text.

A section ``\textbf{Entitled XYZ}'' means a named subunit of the
Document whose title either is precisely XYZ or contains XYZ in
parentheses following text that translates XYZ in another language.
(Here XYZ stands for a specific section name mentioned below, such
as ``\textbf{Acknowledgements}'', ``\textbf{Dedications}'',
``\textbf{Endorsements}'', or ``\textbf{History}''.) To
``\textbf{Preserve the Title}'' of such a section when you modify
the Document means that it remains a section ``Entitled XYZ''
according to this definition.

The Document may include Warranty Disclaimers next to the notice
which states that this License applies to the Document.  These
Warranty Disclaimers are considered to be included by reference in
this License, but only as regards disclaiming warranties: any other
implication that these Warranty Disclaimers may have is void and has
no effect on the meaning of this License.


\begin{center}
{\Large\bf 2. VERBATIM COPYING\par} \phantomsection
\addcontentsline{toc}{section}{2. VERBATIM COPYING}
\end{center}

You may copy and distribute the Document in any medium, either
commercially or noncommercially, provided that this License, the
copyright notices, and the license notice saying this License
applies to the Document are reproduced in all copies, and that you
add no other conditions whatsoever to those of this License.  You
may not use technical measures to obstruct or control the reading or
further copying of the copies you make or distribute.  However, you
may accept compensation in exchange for copies.  If you distribute a
large enough number of copies you must also follow the conditions in
section~3.

You may also lend copies, under the same conditions stated above,
and you may publicly display copies.


\begin{center}
{\Large\bf 3. COPYING IN QUANTITY\par} \phantomsection
\addcontentsline{toc}{section}{3. COPYING IN QUANTITY}
\end{center}


If you publish printed copies (or copies in media that commonly have
printed covers) of the Document, numbering more than 100, and the
Document's license notice requires Cover Texts, you must enclose the
copies in covers that carry, clearly and legibly, all these Cover
Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
the back cover.  Both covers must also clearly and legibly identify
you as the publisher of these copies.  The front cover must present
the full title with all words of the title equally prominent and
visible.  You may add other material on the covers in addition.
Copying with changes limited to the covers, as long as they preserve
the title of the Document and satisfy these conditions, can be
treated as verbatim copying in other respects.

If the required texts for either cover are too voluminous to fit
legibly, you should put the first ones listed (as many as fit
reasonably) on the actual cover, and continue the rest onto adjacent
pages.

If you publish or distribute Opaque copies of the Document numbering
more than 100, you must either include a machine-readable
Transparent copy along with each Opaque copy, or state in or with
each Opaque copy a computer-network location from which the general
network-using public has access to download using public-standard
network protocols a complete Transparent copy of the Document, free
of added material. If you use the latter option, you must take
reasonably prudent steps, when you begin distribution of Opaque
copies in quantity, to ensure that this Transparent copy will remain
thus accessible at the stated location until at least one year after
the last time you distribute an Opaque copy (directly or through
your agents or retailers) of that edition to the public.

It is requested, but not required, that you contact the authors of
the Document well before redistributing any large number of copies,
to give them a chance to provide you with an updated version of the
Document.


\begin{center}
{\Large\bf 4. MODIFICATIONS\par} \phantomsection
\addcontentsline{toc}{section}{4. MODIFICATIONS}
\end{center}

You may copy and distribute a Modified Version of the Document under
the conditions of sections 2 and 3 above, provided that you release
the Modified Version under precisely this License, with the Modified
Version filling the role of the Document, thus licensing
distribution and modification of the Modified Version to whoever
possesses a copy of it.  In addition, you must do these things in
the Modified Version:

\begin{itemize}
\item[A.]
   Use in the Title Page (and on the covers, if any) a title distinct
   from that of the Document, and from those of previous versions
   (which should, if there were any, be listed in the History section
   of the Document).  You may use the same title as a previous version
   if the original publisher of that version gives permission.

\item[B.]
   List on the Title Page, as authors, one or more persons or entities
   responsible for authorship of the modifications in the Modified
   Version, together with at least five of the principal authors of the
   Document (all of its principal authors, if it has fewer than five),
   unless they release you from this requirement.

\item[C.]
   State on the Title page the name of the publisher of the
   Modified Version, as the publisher.

\item[D.]
   Preserve all the copyright notices of the Document.

\item[E.]
   Add an appropriate copyright notice for your modifications
   adjacent to the other copyright notices.

\item[F.]
   Include, immediately after the copyright notices, a license notice
   giving the public permission to use the Modified Version under the
   terms of this License, in the form shown in the Addendum below.

\item[G.]
   Preserve in that license notice the full lists of Invariant Sections
   and required Cover Texts given in the Document's license notice.

\item[H.]
   Include an unaltered copy of this License.

\item[I.]
   Preserve the section Entitled ``History'', Preserve its Title, and add
   to it an item stating at least the title, year, new authors, and
   publisher of the Modified Version as given on the Title Page.  If
   there is no section Entitled ``History'' in the Document, create one
   stating the title, year, authors, and publisher of the Document as
   given on its Title Page, then add an item describing the Modified
   Version as stated in the previous sentence.

\item[J.]
   Preserve the network location, if any, given in the Document for
   public access to a Transparent copy of the Document, and likewise
   the network locations given in the Document for previous versions
   it was based on.  These may be placed in the ``History'' section.
   You may omit a network location for a work that was published at
   least four years before the Document itself, or if the original
   publisher of the version it refers to gives permission.

\item[K.]
   For any section Entitled ``Acknowledgements'' or ``Dedications'',
   Preserve the Title of the section, and preserve in the section all
   the substance and tone of each of the contributor acknowledgements
   and/or dedications given therein.

\item[L.]
   Preserve all the Invariant Sections of the Document,
   unaltered in their text and in their titles.  Section numbers
   or the equivalent are not considered part of the section titles.

\item[M.]
   Delete any section Entitled ``Endorsements''.  Such a section
   may not be included in the Modified Version.

\item[N.]
   Do not retitle any existing section to be Entitled ``Endorsements''
   or to conflict in title with any Invariant Section.

\item[O.]
   Preserve any Warranty Disclaimers.
\end{itemize}

If the Modified Version includes new front-matter sections or
appendices that qualify as Secondary Sections and contain no
material copied from the Document, you may at your option designate
some or all of these sections as invariant.  To do this, add their
titles to the list of Invariant Sections in the Modified Version's
license notice. These titles must be distinct from any other section
titles.

You may add a section Entitled ``Endorsements'', provided it
contains nothing but endorsements of your Modified Version by
various parties--for example, statements of peer review or that the
text has been approved by an organization as the authoritative
definition of a standard.

You may add a passage of up to five words as a Front-Cover Text, and
a passage of up to 25 words as a Back-Cover Text, to the end of the
list of Cover Texts in the Modified Version.  Only one passage of
Front-Cover Text and one of Back-Cover Text may be added by (or
through arrangements made by) any one entity.  If the Document
already includes a cover text for the same cover, previously added
by you or by arrangement made by the same entity you are acting on
behalf of, you may not add another; but you may replace the old one,
on explicit permission from the previous publisher that added the
old one.

The author(s) and publisher(s) of the Document do not by this
License give permission to use their names for publicity for or to
assert or imply endorsement of any Modified Version.


\begin{center}
{\Large\bf 5. COMBINING DOCUMENTS\par} \phantomsection
\addcontentsline{toc}{section}{5. COMBINING DOCUMENTS}
\end{center}


You may combine the Document with other documents released under
this License, under the terms defined in section~4 above for
modified versions, provided that you include in the combination all
of the Invariant Sections of all of the original documents,
unmodified, and list them all as Invariant Sections of your combined
work in its license notice, and that you preserve all their Warranty
Disclaimers.

The combined work need only contain one copy of this License, and
multiple identical Invariant Sections may be replaced with a single
copy.  If there are multiple Invariant Sections with the same name
but different contents, make the title of each such section unique
by adding at the end of it, in parentheses, the name of the original
author or publisher of that section if known, or else a unique
number. Make the same adjustment to the section titles in the list
of Invariant Sections in the license notice of the combined work.

In the combination, you must combine any sections Entitled
``History'' in the various original documents, forming one section
Entitled ``History''; likewise combine any sections Entitled
``Acknowledgements'', and any sections Entitled ``Dedications''. You
must delete all sections Entitled ``Endorsements''.

\begin{center}
{\Large\bf 6. COLLECTIONS OF DOCUMENTS\par} \phantomsection
\addcontentsline{toc}{section}{6. COLLECTIONS OF DOCUMENTS}
\end{center}

You may make a collection consisting of the Document and other
documents released under this License, and replace the individual
copies of this License in the various documents with a single copy
that is included in the collection, provided that you follow the
rules of this License for verbatim copying of each of the documents
in all other respects.

You may extract a single document from such a collection, and
distribute it individually under this License, provided you insert a
copy of this License into the extracted document, and follow this
License in all other respects regarding verbatim copying of that
document.


\begin{center}
{\Large\bf 7. AGGREGATION WITH INDEPENDENT WORKS\par}
\phantomsection \addcontentsline{toc}{section}{7. AGGREGATION WITH
INDEPENDENT WORKS}
\end{center}


A compilation of the Document or its derivatives with other separate
and independent documents or works, in or on a volume of a storage
or distribution medium, is called an ``aggregate'' if the copyright
resulting from the compilation is not used to limit the legal rights
of the compilation's users beyond what the individual works permit.
When the Document is included in an aggregate, this License does not
apply to the other works in the aggregate which are not themselves
derivative works of the Document.

If the Cover Text requirement of section~3 is applicable to these
copies of the Document, then if the Document is less than one half
of the entire aggregate, the Document's Cover Texts may be placed on
covers that bracket the Document within the aggregate, or the
electronic equivalent of covers if the Document is in electronic
form. Otherwise they must appear on printed covers that bracket the
whole aggregate.


\begin{center}
{\Large\bf 8. TRANSLATION\par} \phantomsection
\addcontentsline{toc}{section}{8. TRANSLATION}
\end{center}


Translation is considered a kind of modification, so you may
distribute translations of the Document under the terms of
section~4. Replacing Invariant Sections with translations requires
special permission from their copyright holders, but you may include
translations of some or all Invariant Sections in addition to the
original versions of these Invariant Sections.  You may include a
translation of this License, and all the license notices in the
Document, and any Warranty Disclaimers, provided that you also
include the original English version of this License and the
original versions of those notices and disclaimers.  In case of a
disagreement between the translation and the original version of
this License or a notice or disclaimer, the original version will
prevail.

If a section in the Document is Entitled ``Acknowledgements'',
``Dedications'', or ``History'', the requirement (section~4) to
Preserve its Title (section~1) will typically require changing the
actual title.


\begin{center}
{\Large\bf 9. TERMINATION\par} \phantomsection
\addcontentsline{toc}{section}{9. TERMINATION}
\end{center}


You may not copy, modify, sublicense, or distribute the Document
except as expressly provided for under this License.  Any other
attempt to copy, modify, sublicense or distribute the Document is
void, and will automatically terminate your rights under this
License.  However, parties who have received copies, or rights, from
you under this License will not have their licenses terminated so
long as such parties remain in full compliance.


\begin{center}
{\Large\bf 10. FUTURE REVISIONS OF THIS LICENSE\par} \phantomsection
\addcontentsline{toc}{section}{10. FUTURE REVISIONS OF THIS LICENSE}
\end{center}


The Free Software Foundation may publish new, revised versions of
the GNU Free Documentation License from time to time.  Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.  See
http://www.gnu.org/copyleft/.

Each version of the License is given a distinguishing version
number. If the Document specifies that a particular numbered version
of this License ``or any later version'' applies to it, you have the
option of following the terms and conditions either of that
specified version or of any later version that has been published
(not as a draft) by the Free Software Foundation.  If the Document
does not specify a version number of this License, you may choose
any version ever published (not as a draft) by the Free Software
Foundation. }


\chapter*{Preface}
%\markboth{}{}
%\addcontentsline{toc}{chapter}{Preface} \markright{Preface}

These notes were created because I saw a need to them. There are four main topics that we will cover in the class:

\begin{enumerate}
\item \label{t1} Closed forms for sums and recurrences.
\item \label{t2} Estimates for sums and recurrences.
\item \label{t3} Basic programming algorithms and their complexity.
\item \label{t4} Graph theoretic methods.
\end{enumerate}

Topic \ref{t1} is classic, as I learned from \cite{Boo}, \cite{HaKn} and \cite{Chr}. The more recent \cite{KGP} and \cite{Wil} have also become
instant classics on the subject.


\bigskip

Topic \ref{t2} is very well explained in analytic number theory books (see reference  \cite{MoVa}) and computer science books (once again, refer to \cite{KGP}).
The approaches to asymptotics are somewhat different in the two fields, but nevertheless, both have produced extraordinary methods for
dealing with asymptotic estimates. The classic \cite{DeB} is also worth noting.

\bigskip

There is an abundance of {\em advanced} books for topic \ref{t3}, with \cite{CLRS} and \cite{Knu}  being standard references.
There are very few books, however, that explain basic algorithmic constructs at a level understandable to
a novice, with the notable exceptions  \cite{Ser} and \cite{She}. Their examples are in Pascal or pseudocode, which, for our purposes, will not do. Hence, I have translated
many of their examples into Maple code, and also added many problems of my own.

\bigskip

I haven't included any material on topic \ref{t4} here. There is no shortage of good books, both at the elementary and advanced level in graph theory.
My favourites are \cite{BoMu} and \cite{HaRi}. As the semester progresses, I will write some Maple\TM labs that will include graph theory, and then I
will add them here.


\bigskip

Some of the material here uses Calculus, although Calculus is not part of the course prerequisites. Most of the students taking this course, however, have seen one or two
semesters of Calculus. Those of you not having seen Calculus can skip over those parts. In some cases there are alternative derivations for some of the results here
that do not involve Calculus, but I didn't want to write an encyclopaedic work, and hence I used the most expedient methods available, in many cases using Calculus.
Perhaps some day I will include alternative proofs without Calculus, but I do not foresee having the time.







 \hfill \begin{tabular}{l}David A. SANTOS \\
\href{mailto:dsantos@ccp.edu}{dsantos@ccp.edu} \end{tabular}


\end{frontmatter}



\cfoot{}\rfoot{\psovalbox{\thepage}} \pagestyle{fancy} \fancyhead{}
\renewcommand{\chaptermark}{\markboth{\chaptername\ \thechapter}}
\renewcommand{\sectionmark}{\markright}
\fancyhead[RO]{\itshape\leftmark} \fancyhead[RE]{\itshape\rightmark}

\renewcommand{\headrulewidth}{1pt}
\renewcommand\footrulewidth{1pt}




\chapter{Sums and Recursions}
\pagenumbering{arabic}\setcounter{page}{1}
\Opensolutionfile{discmatII}[discmatII1]

\section{Some Finite Sums and Products}
Recall that by $\dsum _{k=1} ^n a_k$ we mean $a_1+a_2+\cdots + a_n$ and that by
$\prod _{k=1} ^n a_k$ we mean $a_1a_2\cdots a_n$.
We will often write this as
$$ \dsum _{1\leq k \leq n} a_k = a_1+a_2+\cdots + a_n, \qquad \prod _{1\leq k \leq n}a_k =a_1a_2\cdots a_n. $$
Our interest is to obtain {\em closed forms}  for some classic choices of the
$a_k$, that is, a formula for the sum  or the product that is a hopefully simpler expression involving $n$ and not involving sums or products of individual
terms. The are many approaches for obtaining such sums in the simple
cases that we will investigate here. We will only provide a sample
of them. The interested reader may consult the works of \cite{Chr},
\cite{HaKn}, \cite{KGP}, or \cite{Wil} for a more comprehensive
treatment.

\bigskip

Perhaps the simplest cases are when we have
$$(a_2-a_1)+(a_3-a_2)+\cdots + (a_n-a_{n-1})=a_n-a_1,  $$
and
$$ \dfrac{a_2}{a_1}\cdot  \dfrac{a_3}{a_2}\cdots  \dfrac{a_n}{a_{n-1}}= \dfrac{a_n}{a_1},  $$
in which case we say that the sum or the product {\em telescopes}.









\bigskip

We start by adding up a finite geometric series.

\begin{thm}[Finite Geometric Series] Let $x\neq 1$. Then
$\dsum _{0 \leq k \leq n} x^k =1 + x + x^2 + \cdots + x^n =\dfrac{1-x^{n+1}}{1-x}$.
\label{thm:sum-finite-geom-series}\end{thm}
\begin{pf}
Put $$S = 1 + x + x^2 + \cdots + x^n. $$ Then
$$xS = x + x^2 + x^3 + \cdots + x^{n+1} $$shifts every exponent one
unit. Subtracting,
$$S -xS = (1 + x + x^2 + \cdots + x^n) - ( x + x^2 + x^3 + \cdots + x^{n+1}) = 1-x^{n+1} \implies (1-x)S = 1-x^{n+1} \implies S =  \dfrac{1-x^{n+1}}{1-x}, $$
since $x\neq 1$, obtaining the result.
\end{pf}
\begin{rem}
More important than remembering the {\em formula} above is remembering the {\em method} of how this formula was obtained. After many examples it will become clear that the same
method applies to a wide variety of problems: in Mathematics thus there are more problems than methods.
\end{rem}


The above closed form is obtained readily using Maple\TM. You must
press \fbox{ENTER} after entering the semicolon.


\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{sum(x^k,k=0..n);}{}
\end{mapleinput}
\end{maplegroup}





\bigskip
Putting $N=n+1$ in the above formula, we are provided with the
following factorisation, which might be useful in certain
situations.
\begin{equation}
x^N-1 = (x-1)(x^{N-1}+x^{N-2}+\cdots + x +1).
\end{equation}
For example,
$$ x^2-1=(x-1)(x+1), \quad x^3-1=(x-1)(x^2+x+1), \quad x^4-1 = (x-1)(x^3+x^2+x+1), $$
etc. The above simple formula gives rise, upon differentiation, to
other few well known formul\ae.

\begin{cor}\label{cor_derivative-geom-sum}Let $x\neq 1$. Then
$\dsum _{1 \leq k \leq n} kx^{k-1} = \dfrac{1-x^{n+1}}{(1-x)^2}
-\dfrac{(n+1)x^{n}}{1-x}$. \label{cor:sum-finite-geom-arith-series}
\end{cor}
\begin{pf}By Theorem \ref{thm:sum-finite-geom-series} we may set for
$x\neq 1$,  $$f(x)=1+x+ x^2 + \cdots + x^n = \dfrac{1-x^{n+1}}{1-x}.
$$Differentiating both sides,
$$f'(x)=x+ 2x + 3x^2 + \cdots + nx^{n-1} =\dfrac{1-x^{n+1}}{(1-x)^2}
-\dfrac{(n+1)x^{n}}{1-x},
$$obtaining the result.

\bigskip

{\em Aliter:} This is an example of a so-called {\em arithmetic-geometric sequence}. We use the same trick that we used for adding a
geometric sum,
$$S= 1 + 2x + 3x^2 + \cdots + nx^{n-1} \implies xS =  x+2x^2+3x^3+\cdots + nx^n.  $$
Subtracting,
$$ S-xS = 1+ (2x-x)+(3x^2-2x^2)+\cdots + (nx^{n-1}-(n-1)x^{n-1}) - nx^n  = (1+x+x^2+\cdots + x^{n-1})-nx^n = \dfrac{1-x^n}{1-x}-nx^n,$$
upon adding the geometric sum. This reduces to
$$\begin{array}{lll}(1-x)S & = & \dfrac{1-x^n}{1-x}-nx^n\\ & = &  \dfrac{1-x^n-nx^n+nx^{n+1}}{1-x} \\ &  = & \dfrac{1-x^n-nx^n+(n+1)x^{n+1}-x^{n+1}}{1-x}\\
 & = & \dfrac{1-x^{n+1}}{1-x}+\dfrac{-(n+1)x^n+(n+1)x^{n+1}}{1-x}\\
 & = & \dfrac{1-x^{n+1}}{1-x}-x^n\left(\dfrac{(n+1)(1-x)}{1-x}\right)\\
 & = & \dfrac{1-x^{n+1}}{1-x}-(n+1)x^n,
 \end{array} $$from where we get the result.
\end{pf}
The Maple\TM commands to obtain this sum are

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{ sum(k*x^(k-1),k=0..n);}{}
\end{mapleinput}
\end{maplegroup}




\begin{cor}\label{cor:sum-of-first-n-integ} $\dsum _{1 \leq k \leq n} k = \dfrac{n(n+1)}{2}.$
\end{cor}
\begin{pf} We will provide three essentially different proofs for
this classic result. The first proof can be simply obtained by
letting $x=1$ in Corollary \ref{cor:sum-finite-geom-arith-series},
whence
$$\sum _{1 \leq k \leq n} k = \lim _{x\to 1} \left(\dfrac {-x^{n}n+x^{n+1}n-{x}^{n}+1}{ \left(1-x \right) ^{2}}\right) = \dfrac{n(n+1)}{2},  $$
upon using L'H\^{o}pital's Rule twice.

\bigskip
Our second proof is known as Gau\ss\ 's trick. It depends on the fact
that any sum can be added the same forwards as backwards, and since
we are adding an arithmetic progression, the terms at the beginning
compensate the terms at the end to obtain equal quantities. If
$$ S =  1 + 2 + 3 + \cdots + n $$
 then$$ S =  n + (n - 1) +  \cdots + 1.$$Adding these two quantities,
$$ \begin{array}{lcccccccc} S & = & 1 & + &  2 &  + &  \cdots & + & n \\
{S} & {=} & n & + & (n - 1) & + & \cdots & + & 1 \\
\hline
2S & =  & (n + 1) & + & (n + 1) & + & \cdots & + & (n + 1) \\
  & = & n(n + 1), & & & & & \end{array}$$since there are $n$ summands. This
  gives $S = \dis{\dfrac{n(n + 1)}{2}}$, as was to be proved.

\bigskip

For our third proof we convert the given sum into a telescoping sum.
Observe that
$$ k^2 - (k - 1)^2 = 2k - 1.$$
From this
$$
\begin{array}{lcl}
1^2 - 0^2 & = & 2\cdot 1 - 1 \\
2^2 - 1^2 & = & 2\cdot 2 - 1 \\
3^2 - 2^2 & = & 2\cdot 3 - 1 \\

\vdots & \vdots & \vdots \\
n^2 - (n - 1)^2 & = & 2\cdot n - 1
\end{array}
$$
Adding both columns,
$$n^2 - 0^2 = 2(1 + 2 + 3 + \cdots  + n) - n.$$
Solving for the sum,
$$1 + 2 + 3 + \cdots + n =  n^2/2 +  {n}/{2} = \dfrac{n(n + 1)}{2}.$$
\end{pf}

\begin{cor}\label{cor:sum-of-first-n-squares}
$\dsum _{1 \leq k \leq n} k^2 = \dfrac{n(n+1)(2n+1)}{6}.$
\end{cor}
\begin{pf} We will provide two essentially different proofs for
this classic result, which essentially resemble the first and third
proofs of Corollary  \ref{cor:sum-of-first-n-integ}. If in Corollary
\ref{cor_derivative-geom-sum} we put
$$g(x)=  \sum _{1 \leq k \leq n} kx^{k-1} = \dfrac{1-x^{n+1}}{(1-x)^2}
-\dfrac{(n+1)x^{n}}{1-x}$$ then put
$$h(x) =xg(x)=  \sum _{1 \leq k \leq n} kx^{k} = \dfrac{x-x^{n+2}}{(1-x)^2}
-\dfrac{(n+1)x^{n+1}}{1-x}$$ and differentiating,
$$h'(x) =   \sum _{1 \leq k \leq n} k^2x^{k-1} = -{\dfrac {-2\,{x}^{n}n-{x}^{n}+1+x-{x}^{n+1}-{x}^{n}{n}^{2}+2\,{x}^{n+1
}{n}^{2}-{x}^{n+2}{n}^{2}+2\,{x}^{n+1}n}{ \left( -1+x \right)
^{3}}},
 $$and letting $x=1$ we obtain
 $$\sum _{1 \leq k \leq n} k^2 = \lim _{x\to 1}\left( -{\dfrac {-2\,{x}^{n}n-{x}^{n}+1+x-{x}^{n+1}-{x}^{n}{n}^{2}+2\,{x}^{n+1
}{n}^{2}-{x}^{n+2}{n}^{2}+2\,{x}^{n+1}n}{ \left( -1+x \right)
^{3}}}\right)=\dfrac{n^3}{3}+\dfrac{n^2}{2}+\dfrac{n}{6} =
\dfrac{n(n+1)(2n+1)}{6},
$$using L'H\^{o}pital's Rule three times.


\bigskip

For the second proof, observe that
$$ k^3 - (k - 1)^3 = 3k^2 - 3k + 1.$$
Hence
$$
\begin{array}{lcl}
1^3 - 0^3 & = & 3\cdot 1^2 - 3\cdot 1 + 1 \\
2^3 - 1^3 & = & 3\cdot 2^2 - 3\cdot 2 + 1 \\
3^3 - 2^3 & = & 3\cdot 3^2 - 3\cdot 3 + 1 \\
\vdots & \vdots & \vdots \\
n^3 - (n - 1)^3 & = & 3\cdot n^2 - 3\cdot n + 1
\end{array}
$$
Adding both columns,
$$n^3 - 0^3 = 3(1^2 + 2^2 + 3^2 + \cdots  + n^2) - 3(1 + 2 + 3 + \cdots + n) + n.$$
From the preceding example $1 + 2 + 3 + \cdots + n = \cdot n^2/2 +
{n}/{2} = \dfrac{n(n + 1)}{2}$ so

$$n^3 - 0^3 = 3(1^2 + 2^2 + 3^2 + \cdots  + n^2) - \dfrac{3}{2}\cdot n(n + 1) + n.$$
Solving for the sum,
$$1^2 + 2^2 + 3^2 + \cdots + n^2 = \dfrac{n^3}{3}  + \dfrac{1}{2}\cdot n(n + 1) - \dfrac{n}{3}.$$
After simplifying we obtain
$$
1^2 + 2^2 + 3^2 + \cdots + n^2 = \dfrac{n(n + 1)(2n + 1)}{6},
$$as desired.
\end{pf}

The alert reader will note that the leading term in $\dsum _{1\leq k
\leq n}k $ is $\dfrac{n^2}{2}$ and the leading term in $\dsum
_{1\leq k \leq n}k^2 $ is $\dfrac{n^3}{3}$. This is analogous to
$\dint _0 ^n x\d{x} = \dfrac{n^2}{2}$ and $\dint _0 ^n x^2\d{x} =
\dfrac{n^3}{3}$. This is no coincidence, since an integral is
essentially a sum. The Calculus of Finite Differences develops a
``discrete derivative'' and a ``discrete integral'' whereby our sums
can be obtained by a process akin to integration.






The method above of writing a sum as a telescopic sum is the basis
for the Calculus of Finite Differences. A good reference for this is
\cite{Boo}. We present a few more examples using this method.

\begin{thm}
$\dsum _{2\leq k \leq n} \dfrac{1}{(k-1)k}=\dfrac{1}{1\cdot 2} +
\dfrac{1}{2\cdot 3} + \dfrac{1}{3\cdot 4} + \cdots +
\dfrac{1}{(n-1)\cdot n} = \dfrac{n-1}{n} .$ \end{thm}
\begin{pf} Observe that
$$ \dfrac{1}{(k - 1)k} = \dfrac{1}{k-1} - \dfrac{1}{k }.$$Thus
$$
\begin{array}{lcl}
\dfrac{1}{1\cdot 2} & =  &\dfrac{1}{1} - \dfrac{1}{2} \\
\dfrac{1}{2\cdot 3} & =  &\dfrac{1}{2} - \dfrac{1}{3} \\
\dfrac{1}{3\cdot 4} & =  &\dfrac{1}{3} - \dfrac{1}{4} \\
\vdots & \vdots & \vdots \\
\dfrac{1}{(n-1)\cdot n} & =  &\dfrac{1}{n-1} - \dfrac{1}{n}
\end{array}
$$
Adding both columns,
$$\dfrac{1}{1\cdot 2} + \dfrac{1}{2\cdot 3} + \dfrac{1}{3\cdot 4} + \cdots
+ \dfrac{1}{(n-1)\cdot n} = 1 - \dfrac{1}{n} = \dfrac{n-1}{n}.$$
\end{pf}
The alert reader will see how to generalise the method above. For
example, to sum $\dsum _{1\leq k \leq n}
\dfrac{1}{k(k+1)(k+2)(k+3)}$, write the general term as the
difference
$$ \dfrac{1}{k(k+1)(k+2)(k+3)}=\dfrac{1}{3k(k+1)(k+2)}-\dfrac{1}{3(k+1)(k+2)(k+3)}. $$
This gives
$$\begin{array}{lll}\dsum _{1\leq k \leq n}
\dfrac{1}{k(k+1)(k+2)(k+3)}  & = & \dsum _{1\leq k \leq
n}\left(\dfrac{1}{3k(k+1)(k+2)}-\dfrac{1}{3(k+1)(k+2)(k+3)}\right)\\
&  = & \dfrac{1}{3\cdot 1\cdot 2\cdot 3}-\dfrac{1}{2\cdot
(n+1)(n+2)(n+3)}\\
&  = &  \dfrac{1}{18}-\dfrac{1}{3\cdot (n+1)(n+2)(n+3)}.
\end{array}$$
Again, observing the difference
$$ k(k+1) = \dfrac{k(k+1)(k+2)}{3}-\dfrac{(k-1)k(k+1)}{3}, $$
we find
$$\begin{array}{lll}1\cdot 2 + 2\cdot 3 + 3\cdot 4 + \cdots + n(n+1) &
= & \left(\dfrac{1\cdot 2 \cdot 3}{3}-\dfrac{0 \cdot 1 \cdot
2}{3}\right) + \left(\dfrac{2\cdot 3 \cdot 4}{3}-\dfrac{1 \cdot 2
\cdot 3}{3}\right) + \cdots +
\left(\dfrac{n(n+1)(n+2)}{3}-\dfrac{(n-1)n(n+1)}{3}\right)\\
& = & \dfrac{n(n+1)(n+2)}{3}-\dfrac{0 \cdot 1 \cdot 2}{3} \\
& = & \dfrac{n(n+1)(n+2)}{3}.\\
\end{array}$$



\bigskip

The preceding identities were obtained by telescoping cancellation. The idea  can be extended to some products. Here is a classic result.
\begin{thm}
Let $n\geq 1$ be an integer. Then $$ \prod _{k=1} ^n \cos \dfrac{x}{2^{k}}  = \dfrac{\sin x}{2^n\sin \dfrac{x}{2^n}}. $$
\label{thm:telesc-cosines}\end{thm}
\begin{pf}
Using $\sin 2\theta = 2\sin\theta \cos\theta$,  and letting $P= \prod _{k=1} ^n \cos \dfrac{x}{2^{k}} $, we have
$$\begin{array}{lll}  \left(\sin  \dfrac{x}{2^{n}}\right)P& = &  \left(\cos \dfrac{x}{2}\right)\left(\cos \dfrac{x}{2^{2}}\right)\cdots \left(\cos \dfrac{x}{2^n}\right) \left(\sin  \dfrac{x}{2^n}\right)\\
& = &  \left(\cos \dfrac{x}{2}\right)\left(\cos \dfrac{x}{2^{2}}\right)\cdots \left(\cos \dfrac{x}{2^{n-1}}\right)\left(\dfrac{1}{2}\sin \dfrac{x}{2^{n-1}}\right)\\
& = &  \left(\cos \dfrac{x}{2}\right)\left(\cos \dfrac{x}{2^{2}}\right)\cdots \left(\cos \dfrac{x}{2^{n-2}}\right)\left(\dfrac{1}{2^2}\sin \dfrac{x}{2^{n-2}}\right)\\
& = &  \left(\cos \dfrac{x}{2}\right)\left(\cos \dfrac{x}{2^2}\right)\cdots \left(\cos \dfrac{x}{2^{n-3}}\right)\left(\dfrac{1}{2^3}\sin \dfrac{x}{2^{n-3}}\right)\\
& \vdots & \vdots \\
& = & \dfrac{1}{2^{n}}\sin x\\
   \end{array}$$
   From where
$$ \prod _{k=1} ^n \cos \dfrac{x}{2^k} = \dfrac{\sin x}{2^n\sin \dfrac{x}{2^n}}. $$

\end{pf}







For the next discussion we will need the following notation. For
integers $0 \leq k \leq n$, we define the symbol $\binom{n}{k}$
(read {\em $n$ choose $k$}) as follows:
$$\binom{n}{0}=1, \qquad \binom{n}{k}  = \dfrac{n(n-1)(n-2)\cdots (n-k+1)}{k!} =\dfrac{n!}{(n-k)!k!}.$$
For example,
$$ \binom{10}{4} =\dfrac{10\cdot 9 \cdot 8 \cdot 7}{4\cdot 3 \cdot 2 \cdot 1}=210, \qquad \binom{10}{5} = \dfrac{10\cdot 9 \cdot 8 \cdot 7\cdot 6}{5\cdot 4\cdot 3 \cdot 2 \cdot 1}
=252.$$

\begin{thm}[Binomial Theorem] $(1+x)^n=\dsum _{0\leq k \leq n}
\binom{n}{k}x^k$.
\end{thm}
\begin{pf}
We will give the following Calculus based proof, which essentially
computes the MacLaurin expansion of $x\mapsto (1+x)^n$. It is clear
that $(1+x)^n$ is a polynomial of degree $n$, hence put
$$(1+x)^n = a_0 + a_1x +a_2x^2+ \cdots + a_kx^k + \cdots a_nx^n.   $$
We will prove that $a_k=\binom{n}{k}$. Differentiating $k$ times
both sides of the above equality,
$$n(n-1)(n-2) \cdots (n-k+1)(1+x)^{n-k}=k!a_k + (k+1)k(k-1)\cdots 2a_{k+1}x + \cdots + n(n-1)(n-2) \cdots (n-k+1)a_nx^{n-k}.  $$
Setting $x=0$ and noticing that every term after the first vanishes
on the dextral side of the last equality,
$$n(n-1)(n-2) \cdots (n-k+1)=k!a_k \implies a_k = \dfrac{n(n-1)(n-2) \cdots (n-k+1)}{k!} = \binom{n}{k},   $$
as was required.\end{pf} Setting $x=1$ in the identity above we
obtain the following corollary.
\begin{cor} $\binom{n}{0}+\binom{n}{1} + \cdots + \binom{n}{n-1}+\binom{n}{n}=2^n.$
\end{cor}
Since there are $\binom{n}{k}$ subsets of $\{1,2,\ldots , n\}$ with exactly $k$ elements, both sides count the number of
subsets of the set $\{1,2,\ldots , n\}$.
\begin{exa}
How many subsets of $\{1,2,3,\ldots , 100\}$ have an even number (zero included) elements? How many have an odd number of elements.
\end{exa}
\begin{solu}
Recall that $\{1,2,3,\ldots , 100\}$ has $2^{100}=1267650600228229401496703205376$ subsets. Hence, doing a search of them one by one would be silly!
The quantity
$$ \binom{100}{0}+\binom{100}{2}+\binom{100}{4}+\cdots +\binom{100}{98}+ \binom{100}{100}  $$
counts the number of subsets of  $\{1,2,3,\ldots , 100\}$  with an even number of elements, and similarly
$$ \binom{100}{1}+\binom{100}{3}+\binom{100}{5}+\cdots +\binom{100}{97}+ \binom{100}{99}  $$
counts the number of subsets of  $\{1,2,3,\ldots , 100\}$  with an odd number of elements. Set
$$f(x)= (1+x)^{100} = \binom{100}{0}+\binom{100}{1}x+\binom{100}{2}x^2+\binom{100}{3}x^3+\cdots + \binom{100}{99}x^{99}+\binom{100}{100}x^{100}.$$
Then
$$2^{100}=f(1) =  \binom{100}{0}+\binom{100}{1}+\binom{100}{2}+\binom{100}{3}+\cdots + \binom{100}{99}+\binom{100}{100},  $$
and
$$0=f(-1) =  \binom{100}{0}-\binom{100}{1}+\binom{100}{2}-\binom{100}{3}+\cdots - \binom{100}{99}+\binom{100}{100}.  $$
Whence,
$$ \binom{100}{0}+\binom{100}{2}+\binom{100}{4}+\cdots +\binom{100}{98}+ \binom{100}{100}  = \dfrac{f(1)+f(-1)}{2} = 2^{99}=633825300114114700748351602688, $$
and
$$\binom{100}{1}+\binom{100}{3}+\binom{100}{5}+\cdots +\binom{100}{97}+ \binom{100}{99}  =  \dfrac{f(1)-f(-1)}{2} = 2^{99}=633825300114114700748351602688. $$
Incidentally, we have proved that $\{1,2,3,\ldots , 100\}$ has as many subsets with an even number of elements as with an odd number of elements. Try the Maple
sequences

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{sum(binomial(100,2*k),k=0..50);}{}
\mapleinline{active}{1d}{sum(binomial(100, 2*k-1), k = 1 .. 50)}{}
\end{mapleinput}
\end{maplegroup}





\end{solu}



\begin{exa}
Find the exact value of the sum
$$ \dfrac{\binom{10}{1}}{2} + \dfrac{\binom{10}{2}}{3} + \cdots + \dfrac{\binom{10}{10}}{11}.$$
\end{exa}
\begin{solu}
Put $$f(x)=(1+x)^{10} = \binom{10}{0} + \binom{10}{1}x +\binom{10}{2}x^2+\cdots + \binom{10}{10}x^{10}.     $$
Integrating both sides on the interval $[0;1]$ we obtain,
$$\dfrac{2047}{11}=\int _0 ^1 (1+x)^{10} \d{x} =   \dfrac{\binom{10}{0}}{1}+  \dfrac{\binom{10}{1}}{2} + \dfrac{\binom{10}{2}}{3} + \cdots + \dfrac{\binom{10}{10}}{11}, $$
whence
$$  \dfrac{\binom{10}{1}}{2} + \dfrac{\binom{10}{2}}{3} + \cdots + \dfrac{\binom{10}{10}}{11} =  \dfrac{2047}{11}- \dfrac{\binom{10}{0}}{1} = \dfrac{2047}{11}-1=\dfrac{2036}{11}. $$
\end{solu}

\section*{\psframebox{Homework}}
\begin{multicols}{2}\columnseprule 1pt \columnsep 25pt\multicoltolerance=900

\begin{exer}
Here is a standard interview question for prospective computer programmers: You are given a list of $1,000,001$ positive integers from the set
$\{1,2,\ldots , 1,000,000\}$. In your list, every member of  $\{1,2,\ldots , 1,000,000\}$ is listed once, except for $x$, which is listed twice. How do you
find what $x$ is without doing a $1,000,000$ step search?
\begin{answerexer}
Hint: What is $500000500000+x$?
\end{answerexer}
\end{exer}

\begin{exer}
Find the sum of all the integers from $1$ to $1000$ inclusive, which
are not multiples of $3$ or $5$.
\begin{answerexer} We compute the sum
of all integers from $1$ to $1000$ and weed out the sum of the
multiples of $3$ and the sum of the multiples of $5$, but put back
the multiples of $15$, which we have counted twice. Put
$$A_n = 1 + 2 + 3 + \cdots + n,$$
$$B = 3 + 6 + 9 + \cdots + 999 = 3A_{333},$$
$$C = 5 + 10 + 15 + \cdots + 1000 = 5A_{200},$$
$$D= 15 + 30 + 45 +
\cdots + 990 = 15A_{66}.$$ The desired sum is
$$
\begin{array}{lll}
A_{1000} - B - C + D
 & = & A_{1000} - 3A_{333} - 5A_{200} + 15A_{66} \\
& = & 500500 - 3\cdot 55611  - 5\cdot 20100 + 15\cdot 2211 \\
&  = &  266332. \end{array}$$
\end{answerexer}
\end{exer}
\begin{exer}
Find the sum of all integers between $1$ and $100$ that leave
remainder $2$ upon division by $6$.
\begin{answerexer} We want the sum of the integers of the form $6r + 2, r =
0, 1, \ldots , 16.$ But this is
$$\sum _{r = 0} ^{16}(6r + 2) = 6\sum _{r = 0} ^{16} r + \sum _{r = 0} ^{16}2 = 6\frac{16(17)}{2} + 2(17)
= 850.$$
\end{answerexer}
\end{exer}
\begin{exer} The odd natural numbers are arranged as follows:
$$(1)$$
$$(3, 5)$$
$$(7, 9, 11)$$
$$(13, 15, 17, 19)$$
$$(21, 23, 25, 27, 29) $$
$$ ...............................$$
Find the sum of the $n$th row.
\end{exer}

\begin{exer} Shew that
$$1 + 3 + 5 + \cdots + 2n - 1 = n^2.$$\end{exer}
\begin{exer}
Prove using the binomial theorem that $(k+1)^4=k^4+4k^3+6k^2+4k+1$.
Then use the difference
$$ (k+1)^4-k^4=4k^3+6k^2+4k+1 $$and the results of Corollaries
\ref{cor:sum-of-first-n-integ} and \ref{cor:sum-of-first-n-squares}
to prove that
$$ 1^3+2^3+\cdots + n^3 = \left(\dfrac{n(n+1)}{2}\right)^2. $$
\end{exer}
\begin{exer}
A {\em palindrome} is a positive integer whose decimal expansion is symmetric and does not end in $0$. For example,
$1$, $99$, $100123321001$, are all palindromes. Find the sum of all palindromes of five digits, that is, find
$$10001+10101+\cdots + 99999.  $$
\begin{answerexer}
$49500000$.
\end{answerexer}
\end{exer}

\begin{exer} Find a closed formula for
$$D_n = 1 - 2 + 3 - 4 + \cdots + (-1)^{n - 1}n.$$
\begin{answerexer}
$\dfrac{(2n+1)(-1)^{n+1}+1}{4}$.
\end{answerexer}
\end{exer}
\begin{exer} Find a closed formula for
$$T_n = 1^2 - 2^2 + 3^2 - 4^2 + \cdots + (-1)^{n - 1}n^2.$$
\begin{answerexer}
$\dfrac{n(n+1)(-1)^{n+1}}{2}$.
\end{answerexer}

\end{exer}


\begin{exer}
Find a closed form for $\sum _{1\leq k \leq n} 3^k$.
\begin{answerexer}
Use the same method as in theorem \ref{thm:sum-finite-geom-series}: put
$$S =3 + 3^2 + \cdots + 3^n.  $$
Then
$$3S=3^2 +3^3+ \cdots + 3^n+3^{n+1}.$$ Subtracting,
$$3S-S= (3^2 +3^3+ \cdots + 3^n+3^{n+1})-(3 + 3^2 + \cdots + 3^n) = 3^{n+1}-3. $$
 The answer is $\dfrac{3^{n+1}-3}{2}$.
\end{answerexer}
\end{exer}
\begin{exer}Let $n \geq 1$.
Find a closed form for $\sum _{0\leq k \leq n} \binom{n}{k}(-1)^k$.
\begin{answerexer}
By the binomial theorem,  $0=(1-1)^n=\sum _{0\leq k \leq n} \binom{n}{k}(-1)^k$.
\end{answerexer}
\end{exer}
\begin{exer}
Find a closed form for $\sum _{1\leq k \leq n} \binom{n}{k}3^k$.
\begin{answerexer}
By the binomial theorem,  $4^n=(1+3)^n=\sum _{0\leq k \leq n} \binom{n}{k}3^k$,
and so $\sum _{1\leq k \leq n} \binom{n}{k}3^k=4^n-1$.
\end{answerexer}
\end{exer}

\begin{exer}
Evaluate the double sum $\sum _{1\leq i \leq n} \ \sum _{1\leq k \leq n}\ 1$.
\begin{answerexer}
We have
$$\sum _{1\leq i \leq n} \ \sum _{1\leq k \leq n}\ 1 = \sum _{1\leq i \leq n} n = n^2.$$
\end{answerexer}

\end{exer}
\begin{exer}
Evaluate the double sum $\sum _{1\leq i \leq n} \ \sum _{1\leq k \leq i}\ 1$.
\begin{answerexer}
We have
$$\sum _{1\leq i \leq n} \ \sum _{1\leq k \leq i}\ 1 = \sum _{1\leq i \leq n} i = \dfrac{n(n+1)}{2}.$$
\end{answerexer}

\end{exer}
\begin{exer}
Evaluate the double sum $\sum _{1\leq i \leq n} \ \sum _{1\leq k \leq i}\ k$.
\begin{answerexer}
We have
$$\sum _{1\leq i \leq n} \ \sum _{1\leq k \leq i}\ k = \sum _{1\leq i \leq n} \dfrac{i(i+1)}{2} = \dfrac{n(n+1)(n+2)}{6}.$$
\end{answerexer}
\end{exer}
\begin{exer}
Evaluate the double sum $\sum _{1\leq i \leq n} \ \sum _{1\leq k \leq n}\ ik$.
\begin{answerexer}
We have
$$\sum _{1\leq i \leq n} \ \sum _{1\leq k \leq n}\ ik =\left( \sum _{1\leq i \leq n}i\right)\left(  \sum _{1\leq k \leq n}k\right) = \dfrac{n^2(n+1)^2}{4} .$$
\end{answerexer}
\end{exer}
\begin{exer}
Legend says that the inventor of the game of chess, Sissa ben Dahir,  asked the King Shirham of India
to place a grain of wheat on the first square of the
chessboard, $2$ on the second square, $4$ on the third square, $8$
on the fourth square, etc..
\begin{enumerate}
\item  How many grains of wheat are to be
put on the last ($64$-th) square?, \item  How many grains, total, are
needed in order to satisfy the greedy inventor?, \item  Given that $15$
grains of wheat weigh approximately one gramme, what is the
approximate weight, in kg, of wheat needed?, \item  Given that the
annual production of wheat is $350$ million tonnes, how many years,
approximately, are needed in order to satisfy the inventor (assume
that production of wheat stays constant)
\end{enumerate}
\begin{answerexer}\noindent\begin{enumerate}
\item
$2^{63} = 9223372036854775808$, \item  $2^{64} - 1 =
18446744073709551614$, \item  $1.2\times 10^{15}$ kg, or $1200$ billion
tonnes \item  3500 years
\end{enumerate}
\end{answerexer}
\end{exer}

\begin{exer} Factor
$$1 + x + x^2 + \cdots + x^{80}$$
as a polynomial with integer coefficients.
\begin{answerexer} Put $S = 1 + x + x^2 + \cdots + x^{80}.$ Then
$$S - xS = (1 + x + x^2 + \cdots + x^{80}) - (x + x^2 + x^3 + \cdots + x^{80} + x^{81})
= 1 - x^{81},$$or $S(1 - x) = 1 - x^{81}$. Hence
$$1 + x + x^2 + \cdots + x^{80} = \frac{x^{81} - 1}{x - 1}.$$
Therefore
$$\frac{x^{81} - 1}{x - 1} = \frac{x^{81} - 1}{x^{27} - 1}\cdot\frac{x^{27} - 1}{x^{9} - 1}\cdot\frac{x^{9} - 1}{x^3 - 1}\cdot\frac{x^{3} - 1}{x - 1}.$$
Thus
$$1 + x + x^2 + \cdots + x^{80} = (x^{54} + x^{27} + 1)(x^{18} + x^9 + 1)(x^6 + x^3 + 1)(x^2 + x + 1).$$

\end{answerexer}
\end{exer}
\begin{exer}
Prove that $\prod _{k=2} ^n \left(1-\dfrac{1}{k^2}\right)=\dfrac{n+1}{2n}$.
\end{exer}
\begin{exer} Find integers $a, b$ so that
$$ \left(2 + 1\right)\cdot \left(2^2 + 1\right)\cdot\left(2^{2^{2}} + 1\right)
\cdot\left(2^{2^{3}} + 1\right)\cdots \left(2^{2^{99}} + 1\right)
= 2^a + b .$$ \begin{answerexer} Using the identity $x^2 - y^2 =
(x - y)(x + y)$ and letting $P$ be the sought product:
$${\everymath{\displaystyle}\begin{array}{lcr}(2 - 1)P & = & (2 - 1)\left(2 + 1\right)\cdot \left(2^2
+ 1\right)\cdot\left(2^{2^{2}} + 1\right)
\cdot\left(2^{2^{3}} + 1\right)\cdots \left(2^{2^{99}} + 1\right) \\
& = & \left(2^2 - 1\right)\cdot \left(2^2 +
1\right)\cdot\left(2^{2^{2}} + 1\right)
\cdot\left(2^{2^{3}} + 1\right)\cdots \left(2^{2^{99}} + 1\right) \\
& = & \left(2^{2^{2}} - 1\right)\cdot\left(2^{2^{2}} + 1\right)
\cdot\left(2^{2^{3}} + 1\right)\cdots \left(2^{2^{99}} + 1\right) \\
& = & \left(2^{2^{3}} - 1\right)\cdot\left(2^{2^{3}} + 1\right)
\cdot\left(2^{2^{4}} + 1\right)\cdots \left(2^{2^{99}} + 1\right) \\
& \vdots & \vdots \\
& = & (2^{2^{99}} - 1)(2^{2^{99}} + 1) \\
& = & 2^{2^{100}} - 1,
\end{array}}$$ whence
$$P = 2^{2^{100}} - 1.$$
\end{answerexer}
\end{exer}
\begin{exer}
Prove that $$(\log_2 3)(\log_34)(\log_45)\cdots (\log_{1023}1024) =10.$$
\begin{answerexer}
Hints: Using $\log_a b = \dfrac{\log_ca}{\log_cb}$, transform into a telescoping product. $2^{10}=1024$.
\end{answerexer}
\end{exer}



\begin{exer}
Evaluate $\sum _{k = 1} ^{1000} \floor{\log_2 k}$.
\begin{answerexer}
Divide the interval into dyadic (powers of $2$) blocks, and note that $x\mapsto \floor{\log _2 x}$ is constant there.
Thus
$$\begin{array}{lll} \sum _{k = 1} ^{1000} \floor{\log_2 k} & = & \sum _{m = 1} ^{9} \sum _{2^{m-1}<n <2^{m}} \floor{\log_2 n} + \sum _{k =512 } ^{1000} \floor{\log_2 k}\\
& = & \sum _{m = 1} ^{9} \sum _{2^{m-1}<n <2^{m}}(m-1) +  \sum _{k =512 } ^{1000} 9\\
& = & \sum _{m = 1} ^{9} (m-1)2^{m-1} + 489(9)\\
& = & 0\cdot 2^0 + 1\cdot 2^1 +  + 2\cdot 2^2 + 3\cdot 2^3+4\cdot 2^4 + 5\cdot 2^5+ 6\cdot 2^6+ 7\cdot 2^7+ 8\cdot 2^8 +4401\\
& = & 0+2+ 8+24+64+ 160+384+ 896+ 2048+ 4401\\
& = & 7987.
 \end{array} $$
\end{answerexer}
\end{exer}
\begin{exer}
Obtain a closed formula for $\sum _{1\leq k \leq n} k\cdot k!$. Hint: $(k+1)!=(k+1)k!$.
\begin{answerexer}
From the hint: $k\cdot k! = (k+1)! -k!$ and we get the telescoping sum
$$\sum _{1\leq k \leq n} k\cdot k! =  \sum _{1\leq k \leq n} (k+1)! -k!= (2!-1!)+(3!-2!)+(4!-3!)+\cdots ((n+1)!-n!) = (n+1)!-1!.  $$
\end{answerexer}
\end{exer}





\begin{exer}
Prove, by differentiating $x\mapsto (1+x)^n$,  that $\sum _{1\leq k
\leq n} k\binom{n}{k}=n2^{n-1}$.
\begin{answerexer}
Put $f(x)=(1+x)^n=\sum _{0 \leq k \leq n} \binom{n}{k}x^k$. Then
$$f'(x)=n(1+x)^{n-1}=\sum _{0 \leq k \leq n} k\binom{n}{k}x^{k-1} =\sum _{1 \leq k \leq n} k\binom{n}{k}x^{k-1}, $$
since the term $k=0$ vanishes. The result follows upon taking $x=1$.
\end{answerexer}
\end{exer}

\begin{exer}
Prove that $$\dsum _{1\leq k \leq n} k^2\binom{n}{k}
=2^{n-2}n^2+2^{n-2}n.$$
\begin{answerexer}
Put $f(x)=(1+x)^n=\sum _{0 \leq k \leq n} \binom{n}{k}x^k$. Then
$$f'(x)=n(1+x)^{n-1}=\sum _{0 \leq k \leq n} k\binom{n}{k}x^{k-1} =\sum _{1 \leq k \leq n} k\binom{n}{k}x^{k-1}, $$
since the term $k=0$ vanishes. Put now
$$g(x)=xf'(x)=nx(1+x)^{n-1} =\sum _{1 \leq k \leq n} k\binom{n}{k}x^{k}, $$
and so
$$g'(x)=n(1+x)^{n-1}+n(n-1)x(1+x)^{n-2} =\sum _{1 \leq k \leq n} k^2\binom{n}{k}x^{k-1}, $$
The result follows upon taking $x=1$.
\end{answerexer}
\end{exer}
\begin{exer}
Prove that
$$\sum _{0 \leq k \leq \floor{n/2}}\binom{n}{2k}=\binom{n}{0}+\binom{n}{2}+\binom{n}{4}+\cdots = 2^{n-1}, $$ and that $$
\sum _{0 \leq k \leq \floor{n/2}}\binom{n}{2k+1}= \binom{n}{1}+\binom{n}{3}+\binom{n}{5}+\cdots = 2^{n-1}.  $$
(The first sum goes over all binomial coefficients with even index,
the second, over the odd indices.)\end{exer}

\begin{exer} Find the sum of all the coefficients once the following product is expanded
and like terms are collected:
$$(1 - x^2 + x^4)^{109}(2 - 6x + 5x^9)^{1996}.$$
\begin{answerexer} Put
$$ p(x) = (1 - x^2 + x^4)^{109}(2 - 6x + 5x^9)^{1996}.$$Observe that  $p(x)$
is a polynomial of degree $4\cdot 109 + 9\cdot 1996 = 18400.$ Thus
$p(x)$ has the form

$$p(x) = a_0 + a_1x + a_2x^2 + \cdots + a_{18400}x^{18400}.$$The sum of all the coefficients of
$p(x)$ is
$$p(1) = a_0 + a_1 + a_2 + \cdots + a_{18400},$$which is also
$p(1) = (1 - 1^2 + 1^4)^{109}(2 - 6 + 5)^{1996} = 1.$ The desired
sum is thus $1$.
\end{answerexer}
\end{exer}
\begin{exer}
Consider the polynomial
$$(1 - x^2 + x^4)^{2003} = a_0 + a_1x+a_2x^2 + \cdots +
a_{8012}x^{8012}.
$$Find
\begin{dingautolist}{202}
\item $a_0$ \item $a_0 + a_1+a_2 + \cdots + a_{8012}$ \item $a_0 -
a_1+a_2 -a_3 + \cdots -a_{8011}+ a_{8012}$
 \item $a_0+a_2 + a_4 +  \cdots + a_{8010} + a_{8012}$ \item
 $a_1+a_3+ \cdots +a_{8009}+a_{8011}$
\end{dingautolist}
\begin{answerexer} Put $$p(x) = (1 - x^2 + x^4)^{2003} = a_0 + a_1x+a_2x^2 +
\cdots + a_{8012}x^{8012}.$$Then
\begin{dingautolist}{202}
\item $a_0 = p(0) = (1 - 0^2 + 0^4)^{2003} = 1.$ \item $a_0 +
a_1+a_2 + \cdots + a_{8012} = p(1) = (1 - 1^2 + 1^4)^{2003} = 1.$
 \item $$\begin{array}{lll}a_0 -
a_1+a_2-a_3 + \cdots -a_{8011} + a_{8012} &= &  p(-1)\\ & = & (1 -
(-1)^2 + (-1)^4)^{2003}\\ & = & 1. \end{array}$$
\item The required sum is $\dfrac{p(1)+p(-1)}{2} = 1$.
\item The required sum is $\dfrac{p(1)-p(-1)}{2} = 0$.
\end{dingautolist}
\end{answerexer}
\end{exer}
\begin{exer}
Let $f$ satisfy $$f(n + 1) = (-1)^{n + 1}n - 2f(n), \quad n \geq 1.$$ If
$f(1) = f(1001)$ find$$f(1) + f(2) + f(3) + \cdots + f(1000).
$$
\begin{answerexer} We have \\



\begin{tabular}{lllll}
$f(2)$ & = & $(-1)^21 - 2f(1)$ & = & $1 - 2f(1)$ \\
$f(3)$ & = & $(-1)^32 - 2f(2)$ & = & $-2 - 2f(2)$ \\
$f(4)$ & = & $(-1)^43 - 2f(3)$ & = & $3 - 2f(3)$ \\
$f(5)$ & = & $(-1)^54 - 2f(4)$ & = & $-4 - 2f(4)$ \\
$\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ \\
$f(999)$ & = & $(-1)^{999}998 - 2f(998)$ & = & $-998 - 2f(998)$ \\
$f(1000)$ & = & $(-1)^{1000}999 - 2f(999)$ & = & $999 - 2f(999)$ \\
$f(1001)$ & = & $(-1)^{1001}1000 - 2f(1000)$ & = & $-1000 - 2f(1000)$ \\
\end{tabular}


\bigskip
Adding columnwise,
$$f(2) + f(3) + \cdots + f(1001) = 1 - 2 + 3 - \cdots + 999 - 1000 - 2(f(1) + f(2)
+ \cdot + f(1000)).$$ This gives
$$2f(1) + 3(f(2) + f(3) + \cdots + f(1000)) + f(1001) = - 500.$$
Since $f(1) = f(1001)$ we have $2f(1) + f(1001) = 3f(1).$
Therefore$$f(1) + f(2) + \cdots + f(1000)  = - \frac{500}{3}.$$
\end{answerexer}
\end{exer}

\begin{exer} Prove the following identity of Catalan:
$$ 1 - \frac{1}{2} + \frac{1}{3} - \frac{1}{4} + \cdots + \frac{1}{2n - 1} - \frac{1}{2n}
= \frac{1}{n + 1} + \frac{1}{n + 2} + \cdots + \frac{1}{2n}.$$
\begin{answerexer}
 The quantity on the sinistral side is
$${\everymath{\displaystyle}\begin{array}{lcl}
\left( 1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + \cdots + \frac{1}{2n - 1} + \frac{1}{2n}\right) & & \\
\qquad - 2\left(\frac{1}{2} + \frac{1}{4} + \frac{1}{6} + \cdots + \frac{1}{2n}\right) & & \\
& = &
\left( 1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + \cdots + \frac{1}{2n - 1} + \frac{1}{2n}\right)  \\
& & \qquad - 2\cdot\frac{1}{2}\left(1 + \frac{1}{2} + \frac{1}{3} +  \frac{1}{4} + \cdots + \frac{1}{n}\right)       \\
& = &
\left( 1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + \cdots + \frac{1}{2n - 1} + \frac{1}{2n}\right)  \\
& & \qquad - \left(1 + \frac{1}{2} + \frac{1}{3} +  \frac{1}{4} + \cdots + \frac{1}{n}\right)      \\
& = & \frac{1}{n + 1} + \frac{1}{n + 2} + \cdots + \frac{1}{2n},
\end{array}}$$
as we wanted to shew.
\end{answerexer}
\end{exer}


\begin{exer}
Find $$(123456789)^2-(123456787)\cdot (123456791),$$ {\em mentally}.
\begin{answerexer}
If $x=123456789$, then $(123456789)^2-(123456787)\cdot
(123456791)=x^2-(x-2)(x+2)=4$.
\end{answerexer}
\end{exer}
\begin{exer} Given that $ 1002004008016032$ has a prime factor  $p > 250000,$ find it.
\begin{answerexer}
If $a =
10^3 , b = 2$ then $$ 1002004008016032 = a^5 + a^4 b + a^3 b^2 +
a^2 b^3 + ab^4 + b^5 = \frac{ a^6 - b^6 }{ a - b }. $$ This last
expression factors as $$ {\everymath{\displaystyle}
\begin{array}{lcl}\frac{a^6 -
b^6}{a - b} & = & (a + b)(a^2 + ab + b^2)(a^2 - ab + b^2 ) \\
&  = & 1002\cdot 1002004\cdot 998004 \\
& = & 4\cdot 4\cdot 1002\cdot 250501 \cdot k,\end{array} }$$where
$k < 250000$. Therefore $p = 250501$.

\end{answerexer}
\end{exer}

\begin{exer} Shew that
$$\csc 2 + \csc 4 + \csc 8 + \cdots + \csc 2^n = \cot 1 - \cot 2^n.$$
\begin{answerexer}
Shew first that $\csc 2x = \cot x - \cot 2x.$ Use telescoping cancellation.

\end{answerexer}
\end{exer}
\begin{exer} Find the exact value of the product
$$ P = \cos \frac{\pi}{7}\cdot\cos \frac{2\pi}{7}\cdot\cos \frac{4\pi}{7}.$$
\begin{answerexer} Multiplying both sides by $\sin \frac{\pi}{7}$ and using
$\sin 2x = 2 \sin x \cos x$ we obtain
$${\everymath{\displaystyle}\begin{array}{lcr}
\sin \frac{\pi}{7}P & = & (\sin \frac{\pi}{7}\cos \frac{\pi}{7})\cdot\cos \frac{2\pi}{7}\cdot\cos \frac{4\pi}{7} \\
& = & \frac{1}{2}(\sin \frac{2\pi}{7}\cos \frac{2\pi}{7})\cdot\cos \frac{4\pi}{7} \\
& = & \frac{1}{4}(\sin \frac{4\pi}{7}\cos \frac{4\pi}{7}) \\
& = & \frac{1}{8}\sin \frac{8\pi}{7}.


\end{array}}$$
As $\sin \frac{\pi}{7} = -\sin \frac{8\pi}{7}$, we deduce that
$$P = - \frac{1}{8}.$$
\end{answerexer}
\end{exer}
\begin{exer}
Shew that $$\frac{1}{2}\cdot\frac{3}{4}\cdot\frac{5}{6}\cdots
\frac{9999}{10000} < \frac{1}{100}.$$
\begin{answerexer} Let
$$A = \frac{1}{2}\cdot\frac{3}{4}\cdot\frac{5}{6}\cdots \frac{9999}{10000} $$
and
$$ B =\frac{2}{3}\cdot\frac{4}{5}\cdot\frac{6}{7}\cdots\frac{10000}{10001}.$$


Clearly, $x^2 - 1 < x^2$  for all real numbers $x$. This implies
that
$$\frac{x - 1}{x} < \frac{x}{x + 1}$$ whenever these four quantities are
positive. Hence
$${\everymath{\displaystyle}\begin{array}{ccc}
{1}/{2} & < & {2}/{3} \\
{3}/{4} & < & {4}/{5} \\
{5}/{6} & < & {6}/{7} \\
\vdots & \vdots & \vdots \\
{9999}/{10000} & < & {10000}/{10001} \\
\end{array} }$$
As all the numbers involved are positive, we multiply both columns
to obtain
$$\frac{1}{2}\cdot\frac{3}{4}\cdot\frac{5}{6}\cdots \frac{9999}{10000}
<
\frac{2}{3}\cdot\frac{4}{5}\cdot\frac{6}{7}\cdots\frac{10000}{10001},$$
or $A < B.$ This yields $A^2 = A\cdot A < A\cdot B.$ Now
$$A\cdot B = \frac{1}{2}\cdot\frac{2}{3}\cdot\frac{3}{4}\cdot\frac{4}{5}\cdot\frac{5}{6}\cdot\frac{6}{7}
\cdot\frac{7}{8}\cdots\frac{9999}{10000}\cdot\frac{10000}{10001} =
\frac{1}{10001},$$ and consequently, $A^2 < A\cdot B = 1/10001.$
We deduce that $A < 1/\sqrt{10001} < 1/100.$
\end{answerexer}
\end{exer}
\begin{exer}Let $a_1, a_2, \ldots , a_n$ be arbitrary numbers. Shew that

$$
\begin{array}{l}
a_1 + a_2(1 + a_1) + a_3(1 + a_1)(1 + a_2) \\ \quad + a_4(1 + a_1)(1 + a_2)(1 + a_3)  + \cdots \\
\quad + a_{n - 1}(1 + a_1)(1 + a_2)(1 + a_3)\cdots (1 + a_{n - 2})  \\
\qquad\ = (1 + a_1)(1 + a_2)(1 + a_3)\cdots (1 + a_n) - 1.
\end{array}$$
\end{exer}
\begin{exer} Shew that
$$\tan \frac{\pi}{2^{100}} + 2\tan \frac{\pi}{2^{99}} + 2^{2}\tan \frac{\pi}{2^{2^{98}}} +
\cdots + 2^{98}\tan \frac{\pi}{2^2} = \cot
\frac{\pi}{2^{100}}.$$
\end{exer}
\begin{exer} Shew that
$$\sum _{k = 1} ^n \frac{k}{k^4 + k^2 + 1} = \frac{1}{2}\cdot\frac{n^2 + n}{n^2 + n + 1}.$$
\end{exer}


\begin{exer}[Lagrange's Identity]\label{pro:lagranges-id} Let $a_k,
b_k$ be real numbers. Prove that $$ \left(\sum _{k=1} ^na_kb_k\right)^2
=\left(\sum _{k=1} ^na_k ^2\right)\left(\sum _{k=1} ^nb_k
^2\right)-\sum _{1\leq k <j\leq n}(a_kb_j-a_jb_k)^2.$$
\begin{answerexer}
For $j=k$, $a_kb_j-a_jb_k=0$, so we may relax the inequality in the
last sum.  We have
$$\begin{array}{lll}\sum _{1\leq k <j\leq n}(a_kb_j-a_jb_k)^2 & = &
\sum _{1\leq k \leq j\leq n}(a_k ^2b_j ^2
-2a_kb_ka_jb_j+a_j ^2b_k ^2)\\
& = & \sum _{1\leq k \leq j\leq n}a_k ^2b_j ^2
-2\sum _{1\leq k \leq j\leq n} a_kb_ka_jb_j+\sum _{1\leq k \leq j\leq n} a_j ^2b_k ^2\\
& = & \sum _{k=1} ^n\sum _{j=1} ^n a_k ^2 b_j ^2-\left( \sum _{k=1}
^na_kb_k\right)^2,\end{array}  $$proving the theorem.
\end{answerexer}
\end{exer}


\begin{exer}
The sum of a certain number of consecutive positive integers is
$1000$. Find these integers. (There is more than one solution. You
must find them all.)
\begin{answerexer} Let the the sum of integers  be $S = (l + 1) +  (l + 2) + (l
+ n)$. Using Gauss' trick we obtain $S = \dis{\frac{n(2l + n +
1)}{2}}$. As $S = 1000,$ $2000 = n(2l + n + 1)$. Now $2000 = n^2 +
2ln + n > n^2$, whence $n \leq \lfloor\sqrt{2000}\rfloor = 44$.
Moreover, $n$ and $2l + n + 1$  are divisors of $2000$ and are of
opposite parity. Since $2000 = 2^45^3$, the odd factors of $2000$
are $1$, $5$, $25$, and $125$. We then see that the problem has the
following solutions:
$$n = 1, \ l = 999,$$
$$n = 5, \ l = 197,$$
$$n = 16, \ l = 54,$$
$$n = 25, \ l = 27.$$
\end{answerexer}
\end{exer}


\end{multicols}


\section{Some Infinite Sums and Products}
The material of this section will be treated {\em formally}, that is, without much rigor. We present here without proof, the following MacLaurin expansions, which we hope the reader has encountered in his Calculus courses.
\begin{thm}
The following expansions hold:
\begin{enumerate}
\item $\dfrac{1}{1-x}=\sum _{n=0} ^{+\infty}x^n=1+x+x^2+x^3+\cdots , \quad |x|<1$
\item $\sin x =\sum _{n=0} ^{+\infty}\dfrac{(-1)^nx^{2n+1}}{(2n+1)!}= x  - \dfrac{x^3}{3!} + \dfrac{x^5}{5!} - \cdots + (-1)^n\dfrac{x^{2n + 1}}{(2n + 1)!} +  \cdots ,\quad x\in \BBR $.
 \item $\cos x =\sum _{n=0} ^{+\infty}\dfrac{(-1)^nx^{2n}}{(2n)!} =1 -
\dfrac{x^2}{2!} + \dfrac{x^4}{4!} - \cdots +
(-1)^n\dfrac{x^{2n}}{(2n)!} +  \cdots  ,\quad x\in \BBR $. \item $e^x =\sum _{n=0} ^{+\infty}\dfrac{x^n}{n!}= 1 + x
+ \dfrac{x^2}{2!} +  \dfrac{x^3}{3!} + \cdots + \dfrac{x^{n}}{n!}
+  \cdots , \quad x\in \BBR $
\item $\log  (1 + x) =\sum _{n=1} ^{+\infty}\dfrac{(-1)^{n+1}x^n}{n}= x  - \dfrac{x^2}{2} + \dfrac{x^3}{3} -
\cdots  + (-1)^{n + 1}\dfrac{x^n}{n} +  \cdots ,\quad |x|<1$. \item $(1 +
x)^{\tau} = \sum _{n=0} ^{+\infty}\binom{\tau}{n}x^n=1 + \tau x   + \dfrac{\tau (\tau - 1)}{2!}x^2 + \cdots +
\dfrac{\tau (\tau - 1)(\tau - 2) (\tau - 3)\cdots (\tau - n +
1)}{n!}x^n  +  \cdots ,\quad |x|<1$.
\end{enumerate}
\label{thm:maclaurin}\end{thm}

The idea of the preceding section of finding a
general function and then evaluating it a particular value extends to infinite sums, but care must be taken with convergence.
We state the following without proof.
\begin{thm}[Abel's Limit Theorem] Let $r > 0,$ and suppose that $\sum
_{n \geq 0} a_nr^n$ converges. Then $\sum _{n \geq 0}a_nx^n$
converges absolutely for $|x| < r$, and
$$  \lim _{x \rightarrow r^-}\ \sum _{n \geq 0} a_nx^n = \sum _{n \geq 0} a_nr^n.$$
\label{thm:abel}\end{thm}
\begin{exa} Find the exact numerical value of the alternating harmonic series $$ \sum _{n \geq 1} \frac{(-1)^{n - 1}}{n} =
1-\dfrac{1}{2}+\dfrac{1}{3}-\dfrac{1}{4}+\cdots.$$ \end{exa}
\begin{solu} This alternating series converges by Leibniz's Test.
Consider more generally the MacLaurin expansion of $x\mapsto \log (1+x)$:
$$ f(x) = \sum _{n \geq 1} \frac{(-1)^{n - 1}x^n}{n} = \log (1 + x).$$We see that $f(1) = \log 2.$ Thus
$$ 1 - \frac{1}{2} + \frac{1}{3} - \frac{1}{4} + \cdots = \log 2,$$by Abel's Limit Theorem.
The Maple\TM commands to obtain this sum are

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{ sum(((-1)^(k+1))/(k),k=1..infinity);}{}
\end{mapleinput}
\end{maplegroup}
\end{solu}

We now consider an infinite product.
Letting  $\ngroes$ in the product of theorem \ref{thm:telesc-cosines}, we deduce the following result.
\begin{thm}
$$ \prod _{k=1} ^{+\infty} \cos \dfrac{x}{2^k}=\lim_{\ngroes}\prod _{k=1} ^n \cos \dfrac{x}{2^k} = \lim _{\ngroes}\dfrac{\sin x}{2^n\sin \dfrac{x}{2^n}} = \dfrac{\sin x}{x}. $$
\end{thm}
Letting $x=\dfrac{\pi}{2}$ we obtain one of the earliest formulas form $\pi$.

\begin{cor}[Vieta's Formula for $\pi$]
$$ \dfrac{2}{\pi} = \left(\dfrac{\sqrt{2}}{2}\right) \left(\dfrac{\sqrt{2+\sqrt{2}}}{2}\right) \left(\dfrac{\sqrt{2+\sqrt{2+\sqrt{2}}}}{2}\right) \cdots .$$
\end{cor}

Some infinite sums can be recognised as being Riemann sums, and hence, allowing one to sum them. In general,
\begin{equation} \int _a ^b f(x)\d{x} = \lim _{\ngroes}\dfrac{b-a}{n}\sum _{k=0} ^n f\left(a+\dfrac{k(b-a)}{n}\right),  \label{eq:riemann-sum}\end{equation}
if $f$ is Riemann-integrable function on $[a;b]$.

\begin{exa}
Find $\lim _{\ngroes} \sum _{k=0} ^n \dfrac{n}{n^2+k^2}$.
\end{exa}
\begin{solu}
We have,
$$ \sum _{k=0} ^n \dfrac{n}{n^2+k^2} =  \sum _{k=0} ^n \dfrac{1}{n}\dfrac{1}{1+\dfrac{k^2}{n^2}}\to \int _0 ^1 \dfrac{\d{x}}{1+x^2} = \dfrac{\pi}{4},  $$ If $f(x)=\dfrac{1}{1+x^2}$, $a=0$, $b=1$, by
(\ref{eq:riemann-sum}).
\end{solu}



\section*{\psframebox{Homework}}
\begin{multicols}{2}\columnseprule 1pt \columnsep 25pt\multicoltolerance=900
\begin{exer} A fly starts at the origin and goes $1$ unit up, $1/2$ unit right, $1/4$ unit down, $1/8$ unit left,
$1/16$ unit up, etc., {\em ad infinitum.} In what coordinates does
it end up?\begin{answerexer} Its $x$ coordinate is
$$\frac{1}{2} - \frac{1}{8} + \frac{1}{32} - \cdots
= \frac{\frac{1}{2}}{1 - \frac{-1}{4}} = \frac{2}{5}.$$ Its $y$
coordinate is
$$1 - \frac{1}{4} + \frac{1}{16} - \cdots = \frac{1}{1 - \frac{-1}{4}} = \frac{4}{5}.$$Therefore, the
fly ends up in $\left(\frac{2}{5}, \frac{4}{5}\right).$
\end{answerexer}
\end{exer}

\begin{exer} Find the exact numerical value of
$$ \sum _{n \geq 0} \frac{(n + 1)^2}{n!}.$$
\begin{answerexer}From the MacLaurin expansion for $x\mapsto e^x$,
$$ f(x) = xe^x = \sum _{n \geq 0} \frac{x^{n + 1}}{n!}.$$Then
$$f'(x) = xe^x + e^x = \sum _{n \geq 0} \frac{(n + 1)x^n}{n!}.$$
Multiplying by $x,$
$$xf'(x) = x^2e^x + xe^x = \sum _{n \geq 0} \frac{(n + 1)x^{n + 1}}{n!}.$$
Differentiating this last equality,
$$xf''(x) + f'(x) = 2xe^x + x^2e^x + xe^x + e^x = \sum _{n \geq 0} \frac{(n + 1)^2x^n}{n!}.$$
Letting $x \rightarrow 1,$ we obtain
$$ \sum _{n \geq 0} \frac{(n + 1)^2}{n!} = 2e + e + e + e = 5e.$$
\end{answerexer}
\end{exer}


\begin{exer}
Find the exact numerical value of the sum $\sum _{n=1}
^{+\infty}n2^{1-n}$.
\begin{answerexer}
For $|x|<1$,
$$ 1 + x + x^2 + x^3 + \cdots = \dfrac{1}{1-x}.  $$
Differentiating,
$$1 +2x+3x^2+\cdots = \dfrac{1}{(1-x)^2} \implies \sum _{n=1}  ^{+\infty} nx^{n-1} = \dfrac{1}{(1-x)^2}.  $$
Letting $x=\dfrac{1}{2}$,
$$ \sum _{n=1} ^{+\infty} \dfrac{n}{2^{n-1}} = 4.$$
\end{answerexer}


\end{exer}

\begin{exer}
Find the exact numerical value of the sum $\sum _{n=1}
^{+\infty}n^22^{1-n}$.
\begin{answerexer}
For $|x|<1$,
$$ 1 + x + x^2 + x^3 + \cdots = \dfrac{1}{1-x}.  $$
Differentiating,
$$1 +2x+3x^2+\cdots = \dfrac{1}{(1-x)^2}.$$
Multiplying by $x$,
$$x +2x^2+3x^3+\cdots = \dfrac{x}{(1-x)^2}. $$
Differentiating again,
$$1 +4x+9x^2+\cdots = \dfrac{1+x}{(1-x)^3} \implies \sum _{n=1}  ^{+\infty} n^2x^{n-1} = \dfrac{1+x}{(1-x)^3} $$

 Letting $x=\dfrac{1}{2}$,
$$ \sum _{n=1} ^{+\infty} \dfrac{n^2}{2^{n-1}} = 12.$$
\end{answerexer}


\end{exer}

\begin{exer}Let  $\mathscr{S}$ be the set of positive integers none of
whose digits in its decimal representation is a $0$.
Prove that the series $\sum _{n\in \mathscr{S}}\dfrac{1}{n}$
converges.
\begin{answerexer}
We divide the sum into decimal blocks. There are $9^k$ $k$-digit
integers in the interval $[10^k; 10^{k+1}[$ that do not have a $0$
in their decimal representation. Thus
$$\sum _{n\in \mathscr{S}}\dfrac{1}{n} = \sum _{k=0} ^{+\infty} \sum _{n\in [10^k; 10^{k+1}[\cap \mathscr{S}} \dfrac{1}{n}
\leq  \sum _{k=0} ^{+\infty}   9^k\left(\dfrac{1}{10^k}\right) =
10.$$

\end{answerexer}
\end{exer}

\begin{exer}
Find the exact numerical value of the sum $\sum _{n=0}
^{+\infty}\arctan \dfrac{1}{n^2+n+1}$.
\begin{answerexer}
Since $\tan (x-y) = \dfrac{\tan x - \tan y}{1+\tan x\tan y}$,
observe that $\arctan \dfrac{1}{n^2+n+1} = \arctan (n+1) - \arctan
n$. Hence the series telescopes to $\lim _{n\to +\infty}\arctan
(n+1)-\arctan 1 = \dfrac{\pi}{4} .$
\end{answerexer}
\end{exer}
\begin{exer}
Using $\sin 3\theta = 3\sin\theta -4\sin^3\theta$  deduce that
$$ \dfrac{\sin x}{x}=\prod _{n=1} ^{+\infty} \dfrac{4\cos^2\dfrac{x}{3^n}-1}{3}. $$
\end{exer}



\begin{exer}
Find the sum of the series $\sum _{n=1} ^{+\infty}
\dfrac{1}{4n^2-1}$.
\begin{answerexer}Observe that
$$\dfrac{1}{4n^2-1} = \dfrac{1}{2(2n-1)} -\dfrac{1}{2(2n+1)}.$$ Hence
$$\sum _{n=1} ^{+\infty}
\dfrac{1}{4n^2-1} = \left(\dfrac{1}{2(1)} -\dfrac{1}{2(3)}\right) +
\left(\dfrac{1}{2(3)} -\dfrac{1}{2(5)}\right) +
\left(\dfrac{1}{2(5)} -\dfrac{1}{2(7)}\right) + \cdots
=\dfrac{1}{2(1)}  = \dfrac{1}{2}.
$$
\end{answerexer}
\end{exer}
\begin{exer}
Prove that $\prod _{n=2} ^{+\infty} \left(1-\dfrac{1}{n^2}\right)=\dfrac{1}{2}$.
\end{exer}
\begin{exer}
Find the exact numerical value of the infinite sum
$$ \sum _{n=1} ^{+\infty}
\dfrac{\sqrt{(n-1)!}}{(1+\sqrt{1})(1+\sqrt{2})(1+\sqrt{3}) \cdots
(1+\sqrt{n})}.$$
\end{exer}
\begin{exer}
Find
$$1 + \dfrac{1}{2} +\dfrac{1}{3} + \dfrac{1}{6}+\dfrac{1}{8} + \dfrac{1}{9}+\dfrac{1}{12} +\dfrac{1}{16}+\dfrac{1}{18}+\cdots ,$$
which is the sum of the reciprocals of all positive integers of the form
$2^n3^m$ for integers $n \geq 0, m\geq 0$.
\begin{answerexer}
By unique factorisation of the integers, the desired sum is
$$\left(1+\dfrac{1}{2} + \dfrac{1}{2^2}+\dfrac{1}{2^3}+\cdots \right)\left(1+\dfrac{1}{3} + \dfrac{1}{3^2}+\dfrac{1}{3^3}+\cdots \right)
= \dfrac{1}{1-\dfrac{1}{2}}\cdot\dfrac{1}{1-\dfrac{1}{3}} = 3.    $$

\end{answerexer}
\end{exer}

\begin{exer}[Deus Numero Impare Gaudet] Prove that $$ \frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \cdots = \sum _{n\geq 1} \dfrac{(-1)^{n+1}}{2n-1}.$$
\begin{answerexer}We have, using Abel's Theorem
$$ \begin{array}{lll}\dfrac{\pi}{4} & =  & \dint _0 ^1 \dfrac{\d{x}}{1+x^2}\\
& = & \dint _0 ^1 \left(1-x^2+x^4-x^6+x^8-\cdots\right)\d{x} \\
& = & 1-\dfrac{1}{3}+\dfrac{1}{5}-\dfrac{1}{7}+\dfrac{1}{9}-\cdots ,\\
\end{array}$$as wanted. Note: this series was known to Leibniz, for which
he exclaimed that {\em Deus numero impare gaudet}, ``God delights in
odd numbers,'' quoting Virgil.
\end{answerexer}
\end{exer}
\begin{exer} Prove that $$ 1 - \frac{1}{4} + \frac{1}{7} - \frac{1}{10} + \cdots = \frac{1}{3}\left( \log 2 + \frac{\pi}{\sqrt{3}}\right).$$
(Hint: Expand $(1 + x^3)^{-1}$) into a power series. Integrate $(1
+ x^3)^{-1}$ using partial fractions. Use Abel's Limit Theorem.)
\end{exer}

\begin{exer} Let $0 < x < 1.$ Shew that
$$\sum _{n= 1} ^\infty \frac{x^{2^n}}{1 - x^{2^{n + 1}}} = \frac{x}{1 - x}.$$
\begin{answerexer}
Observe that
$$ \frac{y}{1 - y^2} = \frac{1}{1 - y} - \frac{1}{1 - y^2}.
$$
\end{answerexer}
\end{exer}

\begin{exer} Evaluate
$$\left(\frac{1\cdot 2\cdot 4 + 2 \cdot 4\cdot 8 + 3\cdot 6\cdot 12 + \cdots}{
1\cdot 3 \cdot 9 + 2\cdot 6\cdot 18 + 3\cdot 9\cdot 27 +
\cdots}\right) ^{1/3}.$$
\end{exer}
\begin{exer}
Prove that $$\lim _{\ngroes} \sum _{k=1} ^n \dfrac{1}{\sqrt{n^2+k^2}} = \log (1+\sqrt{2}).$$
\begin{answerexer}
By (\ref{eq:riemann-sum}) $$ \lim _{\ngroes} \sum _{k=1} ^n \dfrac{1}{\sqrt{n^2+k^2}} = \lim _{\ngroes} \dfrac{1}{n}\sum _{k=1} ^n \dfrac{1}{\sqrt{1+\dfrac{k^2}{n^2}}} = \int _0  ^1 \dfrac{\d{x}}{\sqrt{1+x^2}}= \log (x+\sqrt{1+x^2})\Big| _0 ^1 =\log (1+\sqrt{2}). $$
\end{answerexer}
\end{exer}
\begin{exer}[Gram's Product]Prove that $$ \prod _{k=2} ^{+\infty} \dfrac{k^3-1}{k^3+1} =\dfrac{2}{3}.$$
\begin{answerexer}
We have
$$\prod _{k=2} ^n \dfrac{k^3-1}{k^3+1} = \prod _{k=2} ^n \dfrac{k-1}{k+1}  \prod _{k=2} ^n \dfrac{k^2+k+1}{k^2-k+1}.  $$
Now
$$\prod _{k=2} ^n \dfrac{k-1}{k+1}  = \dfrac{(n-1)!}{\frac{(n+1)!}{2}} = \dfrac{2}{n(n+1)}.  $$
By observing that  $(k+1)^2-(k+1)+1 = k^2+k+1$, we gather that
$$ \prod _{k=2} ^n \dfrac{k^2-k+1}{k^2+k+1} = \dfrac{3^2+3+1}{2^2-2+1}\cdot \dfrac{4^2+4+1}{3^2+3+1}\cdot \dfrac{5^2+5+1}{4^2+4+1}
\cdots \dfrac{n^2+n+1}{(n-1)^2+(n-1)+1} =\dfrac{n^2+n+1}{3}.  $$
Thus
$$ \prod _{k=2} ^n \dfrac{k^3-1}{k^3+1}  = \dfrac{2}{3} \cdot \dfrac{n^2+n+1}{n(n+1)} \rightarrow
\dfrac{2}{3},$$as $\ngrows$.
\end{answerexer}
\end{exer}

\end{multicols}



\section{Some Identities with Complex Numbers}
We use the symbol $i$
to denote the {\em imaginary unit} $i = \sqrt{-1}$. Then $i^2 =
-1.$  Since $i^0 = 1,\  i^1 = i,$ $i^2 = -1,\ i^3 = -i,$  $i^4 = 1,\ i^5
= i,$ etc., the powers of $i$ repeat themselves cyclically in a
cycle of period $4$.

\begin{exa}
For any  positive integer $\alpha$ one has $$i^\alpha + i^{\alpha + 1} + i^{\alpha +
2} + i^{\alpha + 3} = i^{\alpha}(1 + i + i^2 + i^3) = i^{\alpha}(1
+ i - 1 - i) = 0.$$
\end{exa}






\begin{df}  If $a, b$ are real numbers then the object $z=a + bi$ is
called a {\em complex number}. We use the symbol $\BBC$ to
denote the set of all complex numbers. $a=\Re z$ is the {\em real part} of $z$ and $b=\Im z$ is the {\em imaginary} part of
$z$.
\end{df}
If $a, b, c, d \in {\BBR}$,
then the sum of the complex numbers $a + bi$ and $c + di$ is
naturally defined as
\begin{equation} (a + bi) + (c + di) = (a + c) + (b + d)i \end{equation}
The product of $a + bi$ and $c + di$ is obtained by multiplying
the binomials:
\begin{equation}
(a + bi)(c + di) = ac + adi + bci + bdi^2 = (ac - bd) + (ad + bc)i
\end{equation}


Complex numbers can be given a geometric representation in the {\em Argand diagram} (see figure
\ref{fig:argand}), where the horizontal axis carries the real parts and the vertical axis the imaginary ones.


\vspace{3cm}
\begin{figure}[h]
\begin{minipage}{7cm}
\centering
\psset{unit=1pc}
\psaxes[linewidth=2pt,labels=none,ticks=none]{->}(0,0)(-5,-5)(5,5)
\pstGeonode[PointName=none,linecolor=blue](4;60){A} \uput[ur](A){$a+bi$}
\pstGeonode[PointName=none,PointSymbol=none](0,0){O}(0,5){B}(5,0){C}
\pstLineAB[linecolor=blue]{O}{A}
\pstProjection[PointName=none,linecolor=magenta]{C}{O}{A}[D]
\pstProjection[PointName=none,linecolor=magenta]{B}{O}{A}[E]
\pstLineAB[linecolor=blue]{A}{D}
\pstMarkAngle{C}{O}{A}{$\theta$}
\pscircle[linestyle=dashed,linecolor=red](O){4}
\uput[d](D){$a$}
\uput[l](E){$b$}
\uput[u](B){$\Im$}\uput[u](C){$\Re$}
\psdots[linecolor=blue](A)
\vspace{2cm}\footnotesize\hangcaption{Argand's diagram.}\label{fig:argand}
\end{minipage}
\begin{minipage}{7cm}
\centering
\psset{unit=1pc}
\psaxes[linewidth=2pt,labels=none,ticks=none]{->}(0,0)(-5,-5)(5,5)
\pstGeonode[PointName=none,linecolor=blue](6;55){A} \uput[ur](A){$z$}
\pstGeonode[PointName=none,PointSymbol=none](0,0){O}(0,5){B}(5,0){C}
\pstLineAB[linecolor=blue]{O}{A}\aput{:U}{$|z|$}
\pstProjection[PointName=none,linecolor=magenta]{C}{O}{A}[D]
\pstLineAB[linecolor=blue]{A}{D}  \aput{:D}{$|z|\sin\theta$}
\pstLineAB[linecolor=blue]{O}{D}  \bput{:U}{$|z|\cos\theta$}
\pstMarkAngle{C}{O}{A}{$\theta$}
\uput[u](B){$\Im$}\uput[dr](C){$\Re$}
\psdots[linecolor=blue](A)
\vspace{2cm}\footnotesize\hangcaption{Polar Form of a Complex Number.}\label{fig:polar-form}
\end{minipage}
\end{figure}

\begin{df}
Let $z\in\BBC, (a, b) \in \BBR^2$ with $z = a + bi$. The {\em
conjugate} $\overline{z}$ of $z$ is  defined by
\begin{equation}
\overline{z} = \overline{a + bi} = a - bi
\end{equation}
\end{df}
\begin{rem}
The conjugate of a real number is itself, that is, if $a\in \BBR$,
then $\overline{a} = a$. Also, the conjugate of the conjugate of a
number is the number, that is, $\overline{\overline{z}} = z.$
\end{rem}
\begin{thm}\label{thm:mult-conj}
The function $z: {\BBC} \rightarrow {\BBC}, \ z \mapsto
\overline{z}$ is multiplicative, that is, if $z_1, z_2$ are
complex numbers, then
\begin{equation}
\overline{z_1z_2} = \overline{z_1} \cdot \overline{z_2}
\end{equation}
\end{thm}
\begin{pf} Let $z_1 = a + bi, z_2 = c + di$ where $a, b, c, d$ are real
numbers. Then
$$
\begin{array}{lll}
\overline{z_1z_2} & = & \overline{(a + bi)(c + di)}\\
& = & \overline{(ac - bd) + (ad + bc)i} \\
& = & (ac - bd) - (ad + bc)i\\
\end{array}$$
Also,
$$
\begin{array}{lll}
\overline{z_1}\cdot\overline{z_2} & = & (\overline{a + bi})(\overline{c + di}) \\
& = & (a - bi)(c - di) \\
& = & ac - adi - bci + bdi^2 \\
& = & (ac - bd) - (ad + bc)i,
\end{array}
$$which establishes the equality between the two
quantities. \end{pf}


\begin{df}
The {\em modulus} $|a + bi|$ of $a + bi$ is defined by
\begin{equation}
|a + bi| = \sqrt{(a + bi)(\overline{a + bi})} = \sqrt{a^2 + b^2}
\end{equation}
\end{df}
Observe that $z \mapsto |z|$ is a function mapping  ${\BBC}$ to
$[0;+\infty[$.

\bigskip

Given a complex number $z=a+bi$ on the Argand diagram, consider the angle $\theta \in ]-\pi;\pi ]$  that a straight line segment passing through the origin and through
$z$ makes with  the positive
real axis. Considering the polar coordinates of $z$ we gather
\begin{equation}
z = |z|(\cos \theta + i\sin\theta), \qquad \theta \in ]-\pi; \pi],
\end{equation} which we call the {\em polar form} of the complex number $z$. The angle $\theta$ is called the {\em argument} of the complex number $z$.



\begin{exa}
Find the polar form of $\sqrt{3} - i$.
\end{exa}
\begin{solu}
First observe that $|\sqrt{3}-i|=\sqrt{\sqrt{3}^2+1^2}= 2$. Now, if
$$\sqrt{3}-i=2(\cos \theta+i\sin\theta),$$we need $\cos \theta = \dfrac{\sqrt{3}}{2}$, $\sin \theta = -\dfrac{1}{2}$. This happens for $\theta \in ]-\pi; \pi]$
when $\theta = -\dfrac{\pi}{6}$. Therefore,
$$ \sqrt{3}-i=2(\cos\left(-\dfrac{\pi}{6}\right)  +i\sin\left(-\dfrac{\pi}{6}\right)  $$is the required polar form.
\end{solu}



\begin{thm}\label{thm:mult-of-mod}
The function $z \mapsto |z|,\ {\BBC} \rightarrow [0;+\infty[$ is
multiplicative. That is, if $z_1, z_2$ are complex numbers then
\begin{equation} |z_1z_2| = |z_1||z_2|\end{equation}
\end{thm}
\begin{pf} By Theorem \ref{thm:mult-conj}, conjugation is multiplicative, hence
$$
\begin{array}{lll}
|z_1z_2| & = & \sqrt{z_1z_2\overline{z_1z_2}}\\
& = & \sqrt{z_1z_2\overline{z_1}\cdot\overline{z_2}} \\
& = & \sqrt{z_1\overline{z_1}z_2\overline{z_2}} \\
& = & \sqrt{z_1\overline{z_1}}\sqrt{z_2\overline{z_2}} \\
& = & |z_1||z_2|
\end{array}$$whence the assertion follows. \end{pf}


\begin{exa}
Write $(2^2 + 3^2)(5^2 + 7^2)$ as the sum of two squares.
\end{exa}
\begin{solu} The idea is to write $2^2 + 3^2 = |2 + 3i|^2,\  5^2 +
7^2 = |5 + 7i|^2$ and use the multiplicativity of the modulus.
Now
\begin{center}
\begin{tabular}{lll}
$(2^2 + 3^2)(5^2 + 7^2)$ &  $=$ &  $|2 + 3i|^2|5 + 7i|^2$ \\
&  $=$ & $|(2 + 3i)(5 + 7i)|^2$ \\
&  $=$ & $|-11 + 29i|^2$ \\
& $=$ & $11^2 + 29^2$
\end{tabular}
\end{center}
\end{solu}




We now present some identities involving complex numbers. Let us start with the following classic result.



If we allow complex numbers in our MacLaurin expansions, we readily obtain Euler's Formula.
\begin{thm}[Euler's Formula]
Let $x\in\BBR$. Then
$$ e^{ix} = \cos x + i \sin x. $$\label{thm:euler}
\end{thm}
\begin{pf}
Using the MacLaurin expansion's of $x\mapsto e^x$,   $x\mapsto \cos x$,  and $x\mapsto \sin x$, we gather
$$\begin{array}{lll} e^{ix} & = & \sum _{k=0} ^{+\infty} \dfrac{(ix)^n}{n!} \\
& = & \sum _{k=0} ^{+\infty} \dfrac{(ix)^{2n}}{(2n)!}+ \sum _{k=0} ^{+\infty} \dfrac{(ix)^{2n+1}}{(2n+1)!} \\
& = & \sum _{k=0} ^{+\infty} \dfrac{(-1)^nx^{2n}}{(2n)!}+ i\sum _{k=0} ^{+\infty} \dfrac{(-1)^nx^{2n+1}}{(2n+1)!} \\
& = & \cos x + i\sin x.
 \end{array}$$
\end{pf}





\bigskip

Taking complex conjugates,
$$ e^{-ix}=\overline{e^{ix}}=\overline{\cos x + i\sin x} = \cos x -i\sin x. $$
Solving for $\sin x$ we obtain
\begin{equation}
\sin x = \frac{e^{ix} - e^{-ix}}{2i}
\end{equation}

Similarly,
\begin{equation}
\cos x = \frac{e^{ix} + e^{-ix}}{2}
\end{equation}

\begin{cor}[De Moivre's Theorem] Let $n\in\BBZ$ and $x\in\BBR$. Then $$(\cos x+ i\sin x)^n=\cos nx + i\sin nx$$
\label{cor:demoivre}\end{cor}
\begin{pf}
We have
$$ (\cos x+ i\sin x)^n = (e^{ix})^n = e^{ixn} = \cos nx + i\sin nx, $$by theorem \ref{thm:euler}.

\bigskip
{\em Aliter:} An alternative proof without appealing to Euler's identity follows. We first assume that $n>0$ and give  a proof by induction.
For $n=1$ the assertion is obvious, as
$$ (\cos x+ i\sin x)^1 =  \cos 1\cdot x + i\sin 1\cdot x. $$ Assume the assertion is true for $n-1>1$, that is, assume that
$$(\cos x+ i\sin x)^{n-1}=\cos (n-1)x + i\sin (n-1)x.$$Using the addition identities for the sine and cosine,
$$ \begin{array}{lll} (\cos x+ i\sin x)^n & = & (\cos x+ i\sin x)(\cos x+ i\sin x)^{n-1} \\
& = & (\cos x+ i\sin x)(\cos (n-1)x + i\sin (n-1)x).\\
& = & (\cos x)(\cos (n-1)x)-(\sin x)(\sin (n-1)x) + i((\cos x)(\sin (n-1)x) + (\cos (n-1)x)(\sin x)).\\
& = & \cos (n-1+1)x + i\sin (n-1+1)x\\
& = & \cos nx + i\sin nx,
\end{array}$$proving the theorem for $n>0$.

\bigskip

Assume now that $n<0$. Then $-n>0$ and we may used what we just have proved for positive integers we have
$$\begin{array}{lll} (\cos x+ i\sin x)^n & = &  \dfrac{1}{(\cos x+ i\sin x)^{-n}}\\
& = & \dfrac{1}{\cos (-nx)+i\sin (-nx)}\\
& = & \dfrac{1}{\cos nx -i\sin nx}\\
& = & \dfrac{\cos nx + i\sin nx}{(\cos nx + i\sin nx)(\cos nx - i\sin nx)} \\
& = & \dfrac{\cos nx + i\sin nx}{\cos^2 nx + \sin^2 nx}\\
& = & \cos nx + i\sin nx,
 \end{array}$$proving the theorem for $n<0$. If $n=0$, then since $\sin$ and $\cos$ are not simultaneously
 zero, we get $1=(\cos x+ i\sin x)^0 = \cos 0x+i\sin 0x=\cos 0x=1$, proving the theorem for $n=0$.



\end{pf}
\begin{exa}\label{exa:trisector}
Prove that  $$ \cos 3x = 4\cos ^3x-3\cos x, \qquad \sin 3x = 3\sin
x - 4\sin^3x.
$$
\end{exa}
\begin{solu}
Using Euler's identity and the Binomial Theorem,  $$\begin{array}{lll}\cos 3x + i\sin 3x & = &  e^{3ix}\\
&  = & (e^{ix})^3=(\cos x + i\sin x)^3\\ &  = &
\cos^3x+3i\cos^2x\sin x-3\cos x\sin^2x-i\sin^3x\\
&  = & \cos^3x+3i(1-\sin^2x)\sin x-3\cos
x(1-\cos^2x)-i\sin^3x,\end{array}
$$we gather the required identities.
\end{solu}


The following corollary is immediate.
\begin{cor}[Roots of Unity] If $n>0$ is an integer, the $n$ numbers  $e^{2\pi i k/n}=\cos \dfrac{2\pi k}{n}+i\sin \dfrac{2\pi k}{n}$,  $0 \leq k \leq n-1$, are all different
and    satisfy $(e^{2\pi i k/n}) ^n =1$.
\end{cor}






\begin{exa}
For $n=2$, the square roots of unity are the roots of $$x^2-1=0 \implies x\in \{-1,1\}.$$

\bigskip

For $n=3$ we have $x^3-1=(x-1)(x^2+x+1)=0$ hence if $x\neq 1$ then
$x^2+x+1=0 \implies x=\dfrac{-1\pm i\sqrt{3}}{2}$. Hence the cubic roots of unity are $$\left\{-1, \dfrac{-1- i\sqrt{3}}{2}, \dfrac{-1+ i\sqrt{3}}{2}\right\}.$$
Or, we may find them trigonometrically,
$$\begin{array}{lllll} e^{2\pi i\cdot 0  /3} & = & \cos \dfrac{2\pi \cdot 0}{3} + i\sin \dfrac{2\pi \cdot 0}{3} & = & 1, \\
e^{2\pi i\cdot 1  /3} & = & \cos \dfrac{2\pi \cdot 1}{3} + i\sin \dfrac{2\pi \cdot 1}{3} & = & -\dfrac{1}{2}+i\dfrac{\sqrt{3}}{2} \\
e^{2\pi i\cdot 2  /3} & = & \cos \dfrac{2\pi \cdot 2}{3} + i\sin \dfrac{2\pi \cdot 2}{3} & = &  -\dfrac{1}{2}-i\dfrac{\sqrt{3}}{2} \\
 \end{array} $$
\bigskip


For $n=4$ they are the roots of $x^4-1=(x-1)(x^3+x^2+x+1)=(x-1)(x+1)(x^2+1)=0$, which are clearly
$$ \{-1,1,-i,i\}. $$Or, we may find them trigonometrically,
$$\begin{array}{lllll} e^{2\pi i\cdot 0  /4} & = & \cos \dfrac{2\pi \cdot 0}{4} + i\sin \dfrac{2\pi \cdot 0}{4} & = & 1, \\
e^{2\pi i\cdot 1  /4} & = & \cos \dfrac{2\pi \cdot 1}{4} + i\sin \dfrac{2\pi \cdot 1}{4} & = & i \\
e^{2\pi i\cdot 2  /4} & = & \cos \dfrac{2\pi \cdot 2}{4} + i\sin \dfrac{2\pi \cdot 2}{4} & = &  -1\\
e^{2\pi i\cdot 3  /4} & = & \cos \dfrac{2\pi \cdot 3}{4} + i\sin \dfrac{2\pi \cdot 3}{4} & = & -i \\
 \end{array} $$
\bigskip

For $n=5$ they are the roots of $x^5-1=(x-1)(x^4+x^3+x^2+x+1)=0$. To solve $x^4+x^3+x^2+x+1=0$
observe that since clearly $x\neq 0$, by dividing through by $x^2$, we can transform the equation into
$$ x^2 + \dfrac{1}{x^2} + x+ \dfrac{1}{x}+1 =0. $$
Put now $u =x+ \dfrac{1}{x} $. Then $u^2-2=x^2+\dfrac{1}{x^2}$, and so
$$ x^2 + \dfrac{1}{x^2} + x+ \dfrac{1}{x}+1 =0\implies u^2-2+u+1=0 \implies u=\dfrac{-1\pm \sqrt{5}}{2}.$$
Solving both equations$$ x+ \dfrac{1}{x}= \dfrac{-1- \sqrt{5}}{2}, \qquad x+ \dfrac{1}{x}= \dfrac{-1+ \sqrt{5}}{2},$$
we get the four roots $$ \left\{\dfrac{-1-\sqrt{5}}{4}-i\dfrac{\sqrt{10-2\sqrt{5}}}{4}, \quad \dfrac{-1-\sqrt{5}}{4}+i\dfrac{\sqrt{10-2\sqrt{5}}}{4},
\quad
\dfrac{\sqrt{5}-1}{4}-i\dfrac{\sqrt{2\sqrt{5}+10}}{4}, \quad \dfrac{\sqrt{5}-1}{4}+i\dfrac{\sqrt{2\sqrt{5}+10}}{4}
\right\},  $$ or, we may find, trigonometrically,
$$\begin{array}{lllll} e^{2\pi i\cdot 0  /5} & = & \cos \dfrac{2\pi \cdot 0}{5} + i\sin \dfrac{2\pi \cdot 0}{5} & = & 1, \\
e^{2\pi i\cdot 1  /5} & = & \cos \dfrac{2\pi \cdot 1}{5} + i\sin \dfrac{2\pi \cdot 1}{5} & = & \left(\dfrac{\sqrt{5}-1}{4}\right)+i\left(\dfrac{\sqrt{2}\cdot \sqrt{5+\sqrt{5}}}{4}\right), \\
e^{2\pi i\cdot 2  /5} & = & \cos \dfrac{2\pi \cdot 2}{5} + i\sin \dfrac{2\pi \cdot 2}{5} & = & \left(\dfrac{-\sqrt{5}-1}{4}\right)+i\left(\dfrac{\sqrt{2}\cdot \sqrt{5-\sqrt{5}}}{4}\right), \\
e^{2\pi i\cdot 3  /5} & = & \cos \dfrac{2\pi \cdot 3}{5} + i\sin \dfrac{2\pi \cdot 3}{5} & = & \left(\dfrac{-\sqrt{5}-1}{4}\right)-i\left(\dfrac{\sqrt{2}\cdot \sqrt{5-\sqrt{5}}}{4}\right), \\
e^{2\pi i\cdot 4  /5} & = & \cos \dfrac{2\pi \cdot 4}{5} + i\sin \dfrac{2\pi \cdot 4}{5} & = & \left(\dfrac{\sqrt{5}-1}{4}\right)-i\left(\dfrac{\sqrt{2}\cdot \sqrt{5+\sqrt{5}}}{4}\right), \\
 \end{array} $$

\bigskip
See figures \ref{fig:cbc-rt1} through \ref{fig:qu-rt1}.

\end{exa}



\vspace{2cm}


\begin{figure}[h]
\begin{minipage}{5cm}
\centering
\psset{unit=1pc}
\psaxes[linewidth=2pt,labels=none,ticks=none]{->}(0,0)(-5,-5)(5,5)
\pstGeonode[PointName=none,linecolor=blue,linewidth=2pt](4;0){A}(4;120){B}(4;240){C}
\pspolygon[linewidth=1.2pt,linecolor=red](A)(B)(C)
\pstGeonode[PointName=none,PointSymbol=none](0,0){O}
\pscircle[linestyle=dashed,linecolor=red](O){4}
\vspace{2cm}\footnotesize\hangcaption{Cubic Roots of $1$.}\label{fig:cbc-rt1}
\end{minipage}
\hfill
\begin{minipage}{5cm}
\centering
\psset{unit=1pc}
\psaxes[linewidth=2pt,labels=none,ticks=none]{->}(0,0)(-5,-5)(5,5)
\pstGeonode[PointName=none,linecolor=blue,linewidth=2pt](4;0){A}(4;90){B}(4;180){C}(4;270){D}
\pspolygon[linewidth=1.2pt,linecolor=red](A)(B)(C)(D)
\pstGeonode[PointName=none,PointSymbol=none](0,0){O}
\pscircle[linestyle=dashed,linecolor=red](O){4}
\vspace{2cm}\footnotesize\hangcaption{Quartic  Roots of $1$.}\label{fig:qt-rt1}
\end{minipage}
\hfill
\begin{minipage}{5cm}
\centering
\psset{unit=1pc}
\psaxes[linewidth=2pt,labels=none,ticks=none]{->}(0,0)(-5,-5)(5,5)
\pstGeonode[PointName=none,linecolor=blue,linewidth=2pt](4;0){A}(4;72){B}(4;144){C}(4;216){D}(4;288){E}
\pspolygon[linewidth=1.2pt,linecolor=red](A)(B)(C)(D)(E)
\pstGeonode[PointName=none,PointSymbol=none](0,0){O}
\pscircle[linestyle=dashed,linecolor=red](O){4}
\vspace{2cm}\footnotesize\hangcaption{Quintic  Roots of $1$.}\label{fig:qu-rt1}
\end{minipage}
\end{figure}
By the Fundamental Theorem of Algebra the equation $x^n-1 = 0$ has exactly $n$ complex roots, which gives   the following result.
\begin{cor}\label{cor:decom-of-root-1}
Let $n>0$ be an integer. Then
$$x^n-1 = \prod _{k=0} ^{n-1} (x-e^{2\pi i k/n}).  $$
\end{cor}

\begin{thm}\label{thm:series-multisection} We have,
$$ 1 + x + x^2 + \cdots + x^{n - 1} = \left\{\begin{array}{ll} 0 & x = e^{\frac{2\pi ik}{n}}, \quad  1 \leq k
\leq n - 1, \\
n & x = 1.  \end{array} \right.$$
\end{thm}
\begin{pf}
Since $x^n-1 =(x-1)(x^{n-1}+x^{n-2}+\cdots + x +1)$, from Corollary \ref{cor:decom-of-root-1}, if $x\neq 1$,
$$ x^{n-1}+x^{n-2}+\cdots + x +1 =  \prod _{k=1} ^{n-1} (x-e^{2\pi i k/n}).$$
If $\epsilon $ is a root of unity different from $1$, then $\epsilon =e^{2\pi i k/n}$ for some $k\in [1;n-1]$, and this proves the theorem. Alternatively,
$$ 1 + \epsilon + \epsilon ^2 + \epsilon ^3 + \cdots + \epsilon ^{n - 1} = \frac{\epsilon ^n - 1}{\epsilon - 1} = 0.$$
This gives the result.
\end{pf}
\begin{thm}\label{thm:prod-sines}
Let $n\geq 1$ be an integer. Then $\dfrac{n}{2^{n-1}}=\prod _{k=1} ^{n-1}\sin \dfrac{k\pi}{n}$.
\end{thm}
\begin{pf}
Differentiating both sides of the equality
$$x^n-1 =  \prod _{k=0} ^{n-1} (x-e^{2\pi i k/n}), $$
and letting $x=1$,
$$\begin{array}{lll} n & = & (1-e^{2\pi i/n})(1-e^{4\pi i/n})(1-e^{6\pi i/n})\cdots (1-e^{2(n-1)\pi i/n})\\
& = & e^{(1+2+3+\cdots +(n-1))\pi i/n}(e^{-\pi i/n}-e^{\pi i/n}) (e^{-2\pi i/n}-e^{2\pi i/n})(e^{-3\pi i/n}-e^{3\pi/n})\cdots (e^{-(n-1)\pi i/n}-e^{(n-1)\pi i/n})\\
& = & e^{(n-1)\pi i/2} \left(-2i\sin \dfrac{\pi}{n}\right)\left(-2i\sin \dfrac{2\pi}{n}\right)\cdots \left(-2i\sin \dfrac{(n-1)\pi}{n}\right)\\
& = & e^{(n-1)\pi i/2} (-i)^{n-1}2^{n-1} \prod _{k=1} ^{n-1}\sin \dfrac{k\pi}{n} \\
&= &  (e^{\pi i/2})^{n-1} (-i)^{n-1}2^{n-1} \prod _{k=1} ^{n-1}\sin \dfrac{k\pi}{n} \\
& = & i^{n-1}(-i)^{n-1}2^{n-1} \prod _{k=1} ^{n-1}\sin \dfrac{k\pi}{n} \\
& = & (-i^2)^{n-1}2^{n-1} \prod _{k=1} ^{n-1}\sin \dfrac{k\pi}{n} \\
& = & 2^{n-1} \prod _{k=1} ^{n-1}\sin \dfrac{k\pi}{n}, \\
\end{array}$$giving the result.
\end{pf}

\begin{exa}
Prove that  the improper integral $I=\int _0 ^{\pi} \log \sin x\d{x}=-\pi \log 2$.
\end{exa}
\begin{solu}
We will deduce this in two ways. From Theorem \ref{thm:prod-sines},
$$ \sum _{k=1} ^{n-1} \log \sin \dfrac{k\pi}{n} =\log n-(n-1)\log 2.  $$
By (\ref{eq:riemann-sum}), we see that
$$ \int _0 ^{\pi} \log \sin x\d{x} = \lim _{\ngroes}  \dfrac{\pi}{n}\sum _{k=1} ^{n-1} \log \sin \dfrac{k\pi}{n}=\lim _{\ngroes}  \dfrac{\pi}{n}\left(\log n-(n-1)\log 2\right)=-\pi\log 2,$$
as claimed.

\bigskip

{\em Aliter:} From $\sin x = 2\sin \frac{x}{2}\cos \frac{x}{2}$ we get
$$\begin{array}{lll} I & = & \dint _0 ^{\pi}\log 2 \d{x}+ \dint _0
^{\pi}\log\sin \frac{x}{2} \d{x} + \dint _0 ^{\pi}\log\cos \frac{x}{2}\d{x}\\
& = & \pi\log 2 +  2\dint _0
^{\pi/2}\log\sin y \d{y} + 2\dint _0 ^{\pi /2}\log \cos y\d{y}.\\
\end{array}
$$
Setting $y = \dfrac{\pi}{2}-u$ and using $\sin (\pi-u) =\sin u =
\cos \left(\dfrac{\pi}{2}-u\right)$ we see that $$\dint _0
^{\pi/2}\log\sin y \d{y} =\dint _0 ^{\pi /2}\log \cos y\d{y}
\implies 2\dint _0 ^{\pi/2}\log\sin y \d{y} = \dint _0
^{\pi/2}\left(\log\sin u +\log \sin (\pi-x)\right) \d{u} = \dint _0
^{\pi}\log\sin u \d{u}=I,$$from where
$$I = \pi\log 2 + 2I \implies  I = -\pi\log 2.$$
\end{solu}




\begin{exa}
Justify that $\sum _{n=1} ^{+\infty} \dfrac{\sin n}{n} = \dfrac{\pi -1}{2}$.
\end{exa}
\begin{solu}
We start by assuming that $\sum _{n=1} ^{+\infty}\dfrac{e^{izn}}{n} = -\log (1-e^{iz})$ for $z\in \BBR$, in analogy to the MacLaurin expansion of $x\mapsto \log (1+x)$
for real $x$. Then letting $z=1$,
$$\begin{array}{lll}\sum _{n=1} ^{+\infty} \dfrac{\cos n + i\sin n}{n}& = &  \sum _{n=1}^{+\infty} \dfrac{e^{in}}{n} \\
& = & -\log (1-e^{i}) \\ & = &  -\log e^{i/2}(e^{-i/2}-e^{i/2})\\
& = & -\log e^{i/2}-\log 2i\left(-\sin \frac{1}{2}\right)\\
& = &-\log (-2i) -\dfrac{i}{2}-\log \left(\sin \frac{1}{2}\right)\\
\end{array}$$ Since $-2i=2e^{-\pi i/2}$, $-\log (-2i)=-\log 2+\dfrac{\pi i}{2}$. Thus we get
$$\sum _{n=1} ^{+\infty} \dfrac{\cos n + i\sin n}{n}=-\log 2-\log \left(\sin \frac{1}{2}\right) + i\left(\dfrac{\pi}{2} -\dfrac{1}{2}\right).  $$
Equating real and imaginary parts we verify our claim.

\bigskip

The formal argument above can be rigorously proved by means of Fourier Analysis, but this is beyond our scope.

\end{solu}




Theorem \ref{thm:series-multisection}  is quite useful for ``multisecting'' a power
series.
\begin{exa} Find the sum
 $S = \sum _{k = 0} ^{9} \binom{27}{3k}$.
\label{exa:multi-binom1} \end{exa}
 \begin{solu}
We use the fact that for $\epsilon _1 = -1/2 + i\sqrt{3}/2$
and $\epsilon _2 = -1/2 - i\sqrt{3}/2$ are cubic roots of unity and hence satisfy $$ \epsilon _k ^3 =
1, \ {\rm and} \ 1 + \epsilon _k + \epsilon _k ^2 = 0, \ k = 1,
2.$$ Thus \begin{equation}  \epsilon _k ^{s} + \epsilon _k ^{s
+ 1} + \epsilon _k ^{s + 2} = 0, \ k = 1, 2, \ s\in \BBZ. \label{eq:cubic-rootsof-1}\end{equation} From
this
$${\everymath{\displaystyle} \begin{array}{lcl} (1 + 1)^{27} & = & \binom{27}{0} + \binom{27}{1} + \binom{27}{2} + \binom{27}{4} + \cdots +
\binom{27}{26} + \binom{27}{27} \\
(1 + \epsilon _1)^{27} & = & \binom{27}{0} + \binom{27}{1}\epsilon
_1 + \binom{27}{2}\epsilon _1 ^2 +
\binom{27}{3}\epsilon _1 ^3 + \cdots + \binom{27}{27}\epsilon _1 ^{27} \\
(1 + \epsilon _2)^{27} & = & \binom{27}{0} + \binom{27}{1}\epsilon
_2 + \binom{27}{2}\epsilon _2 ^2 + \binom{27}{3}\epsilon _2 ^3 +
\cdots + \binom{27}{27}\epsilon _2 ^{27}
\end{array}}$$ Summing column-wise and noticing that because of (\ref{eq:cubic-rootsof-1}) only the terms $0, 3, 6, \ldots , 27$ survive,
$$ 2^{27} + (1 + \epsilon _1)^{27} + (1 + \epsilon _2)^{27} = 3\binom{27}{0} + 3\binom{27}{3} + 3\binom{27}{6} +
\cdots + 3\binom{27}{27}. $$ By DeMoivre's Theorem, $(1 - 1/2 +
i\sqrt{3}/2)^{27} = \cos 9\pi + i\sin 9\pi = -1$ and $(1 - 1/2 -
i\sqrt{3}/2)^{27} = \cos 45\pi + i\sin 45\pi = -1$. Thus
$$ \binom{27}{0} + \binom{27}{3} + \binom{27}{6} + \cdots + \binom{27}{27} = \frac{1}{3}(2^{27} - 2).$$

\end{solu}


The  procedure of example \ref{exa:multi-binom1}  can be generalised as follows. Suppose that
$$ f(x) = \sum _{k = 0} ^\infty \ c_kx^k.$$ If $\omega = e^{2\pi i i/q,}, q \in \BBN , q > 1,$ then
$\omega ^q = 1$ and $1 + \omega + \omega ^2 + \omega ^3 + \cdots +
\omega ^{q - 1} = 0.$ Then in view of
$$  \frac{1}{q}\sum _{1 \leq b \leq q} \omega ^{kb} = \left\{\begin{array}{ll}
1 & {\rm if \ } q \ {\rm divides \ } k, \\
0 & {\rm else},
\end{array} \right.$$
we have \begin{equation} \sum _{\stackrel{n = 0}{n \equiv a \ {\rm
mod} \ q}} ^\infty  c_{n}x^n = \frac{1}{q}\sum _{b = 1} ^q \omega
^{-ab}f( \omega ^b x).\end{equation} We may use complex numbers to
select certain sums of coefficients of polynomials. The following
problem uses the fact that if $k$ is an integer
\begin{equation}
i^k + i^{k + 1} + i^{k + 2} + i^{k + 3} = i^k(1 + i + i^2 + i^3) =
0 \end{equation}



\begin{exa} Let
$$(1 + x^4 + x^8)^{100} = a_0 + a_1x + a_2x^2 + \cdots + a_{800}x^{800}.$$
Find: \begin{dingautolist}{202}
\item $a_0 + a_1 + a_2 + a_3 + \cdots + a_{800}.$ \\
\item $a_0 + a_2 +  a_4 + a_6 + \cdots + a_{800}.$  \\
\item $a_1 + a_3 +  a_5 + a_7 + \cdots + a_{799}.$   \\
\item $a_0 + a_4 +  a_8 + a_{12} + \cdots + a_{800}.$ \\
\item $a_1 + a_5 +  a_9 + a_{13} + \cdots + a_{797}.$
\end{dingautolist}
\end{exa}
\begin{solu} Put $$p(x) = (1 + x^4 + x^8)^{100} = a_0 + a_1x + a_2x^2
+ \cdots + a_{800}x^{800}.$$ Then \begin{dingautolist}{202} \item
$$ a_0 + a_1 + a_2 + a_3 + \cdots + a_{800} = p(1) = 3^{100}.
$$ \item $$a_0 + a_2 +  a_4 + a_6 + \cdots + a_{800} = \frac{p(1) +
p(-1)}{2} = 3^{100}. $$ \item $$a_1 + a_3 +  a_5 + a_7 + \cdots +
a_{799} = \frac{p(1) - p(-1)}{2} = 0. $$ \item $$a_0 + a_4 +  a_8
+ a_{12} + \cdots + a_{800} = \frac{p(1) + p(-1) + p(i) +
p(-i)}{4} = 2\cdot 3^{100}.$$ \item
$$a_1 + a_5 +  a_9 + a_{13} + \cdots + a_{797} = \frac{p(1) - p(-1) -ip(i) + ip(-i)}{4} =
0.$$
\end{dingautolist}
\end{solu}


\section*{\psframebox{Homework}}
\begin{multicols}{2}\columnseprule 1pt \columnsep 25pt\multicoltolerance=900
\begin{exer} Compute
$$\dfrac{(1+i)^{2004}}{(1-i)^{2000}}.$$
\begin{answerexer}Observe that
 $(1+i)^2=1+2i+i^2=2i$ and so
$(1+i)^{2004}=2^{1002}i^{1002}=-2^{1002}$. Also,
 $(1-i)^2=1-2i+i^2=-2i$ and so
$(1-i)^{2000}=2^{1000}i^{1000}=2^{1000}$. Hence
$$\dfrac{(1+i)^{2004}}{(1-i)^{2000}} = \dfrac{-2^{1002}}{2^{1000}}=-4$$
\end{answerexer}
\end{exer}
\begin{exer}Let $i^2=-1$.
Evaluate $$1+2i+3i^2+4i^3+5i^4+\cdots + 2007i^{2006}.$$
\begin{answerexer}
Observe that $n+(n+1)i + (n+2)i^2+(n+3)i^3=n+ni+i-n-2-ni-3i=-2-2i$. Thus grouping every four terms,
 $$\begin{array}{lll}1+2i+3i^2+4i^3+5i^4+\cdots + 2007i^{2006}
 & = & (1+2i+3i^2+4i^3)+(5i^{4}+6i^{5} + 7i^{6} +  8i^{7})+\cdots + (2001i^{2000}+2002i^{2001} + 2003i^{2002} +  2004i^{2003})+      2005i^{2004} + 2006i^{2005} +  2007i^{2006}\\
 &  = & \underbrace{(-2-2i)+(-2-2i)+ \cdots + (-2-2i)}_{501\ \mathrm{terms}} +  2005 + 2006i -  2007\\
 & = & -1002-1002i+ 2005 + 2006i -  2007\\
 & = & -1004-1004i .
 \end{array}$$
\end{answerexer}
\end{exer}

\begin{exer}
Prove that
$$\cos^62x = \frac{1}{32}\cos 12x + \frac{3}{16}\cos 8x + \frac{15}{32}\cos 4x + \frac{5}{16}.$$
\begin{answerexer}
Using the binomial theorem and Euler's formula,
$$\begin{array}{lll} 32\cos^6 2x  & = & \left(e^{2ix} + e^{-2ix}\right)^6\\
& = & \binom{6}{0}e^{12ix} +  \binom{6}{1}e^{10ix}e^{-2ix}+  \binom{6}{2}e^{8ix}e^{-4ix}+ \binom{6}{3}e^{6ix}e^{-6ix}+ \binom{6}{4}e^{4ix}e^{-8ix} +  \binom{6}{5}e^{2ix}e^{-10ix} + \binom{6}{6}e^{-12ix}\\
& = & e^{12ix} + 6e^{8ix} + 15e^{4ix} + 20 + 15e^{-4ix} + 6e^{-8ix} + e^{-12ix}\\
& = & (e^{12ix}+e^{-12ix})+6(e^{8ix}+e^{-8ix})+15(e^{4ix} +e^{-4ix}) + 20 \\
& = & 2\cos 12x+ 12\cos 8x + 30\cos 4x + 20,
\end{array}
$$
from where we deduce the result.
\end{answerexer}
\end{exer}

\begin{exer}
Prove that $$ \sqrt{3}=\tan\dfrac{\pi}{9}+4\sin \dfrac{\pi}{9}.$$
\begin{answerexer}
From  $$ \cos 3x = 4\cos ^3x-3\cos x, \qquad \sin 3x = 3\sin
x - 4\sin^3x,
$$we gather, upon using the double angle and the sum identities,
$$ \begin{array}{lll} \renewcommand{\arraystretch}{3.5}
\tan 3x &  =  & \dfrac{3\sin
x-4\sin ^3x}{4\cos^3 x-3\cos x}\\
& = & \tan x
\left(\dfrac{3-4\sin^2x}{4\cos^2x-3}\right)\\
& = & \tan x
\left(\dfrac{3-4\sin^2x}{1-4\sin^2x}\right)\\
& = & \tan x
\left(1 + \dfrac{2}{1-4\sin^2x}\right)\\
& = & \tan x+
\dfrac{2\sin x}{\cos x - 4\sin^2x\cos x}.\\
& = & \tan x+
\dfrac{2\sin x}{\cos x - 2\sin x \sin 2x}\\
& = & \tan x+
\dfrac{2\sin x}{\cos x - 2\left(\dfrac{\cos x}{2}-\dfrac{\cos 3x}{2}\right)}\\
& = & \tan x + \dfrac{2\sin x}{\cos 3x}.
\end{array}$$
Finally, upon letting $x=\dfrac{\pi}{9}$ we gather,
$$ \sqrt{3}=\tan \dfrac{\pi}{3}= \tan\dfrac{\pi}{9}+\dfrac{2\sin \dfrac{\pi}{9}}{\cos \dfrac{\pi}{3}} = \tan\dfrac{\pi}{9}+4\sin \dfrac{\pi}{9},$$
as it was to be shewn.


\end{answerexer}
\end{exer}

\begin{exer} Let $$ (1 + x + x^2)^n = a_0 + a_1 x + a_2 x^2 + \cdots + a_{2n} x^{2n}. $$
Find formul\ae \,\,  for \begin{enumerate} \item
$\displaystyle{\sum _{k = 0} ^{2n} a_k}$ \item $\displaystyle{\sum
_{0 \leq k \leq n/2} a_{2k}}$ \item $\displaystyle{\sum _{1 \leq k
\leq n/2} a_{2k - 1}}$ \item $a_0 + a_4 + a_8 + \cdots$ \item $a_1
+ a_5 + a_9 + \cdots$
\end{enumerate}
\begin{answerexer} Let $f(x) = (1 + x + x^2)^n$. \begin{enumerate}
\item Clearly $a_0 + a_1 + a_2 + a_3 + a_4 + \cdots = f(1) = 3^n.$ \\
\item We have $$ \begin{array}{lcl} f(1) & = &  a_0 + a_1 + a_2 + a_3 + \cdots \\
f(-1) & = & a_0 - a_1 + a_2 - a_3 + \cdots \\ \end{array}$$
Summing these two rows,
$$ f(1) + f(-1) = 2a_0 + 2a_2 + 2a_4 + \cdots ,$$
whence
$$ a_0 + a_2 + a_4 + \cdots = \frac{1}{2}(f(1) + f(-1)) = \frac{1}{2}(3^n + 1).$$
\item We see that $$ f(1) - f(-1) = 2a_1 + 2a_3 + 2a_5 + \cdots$$
Therefore
$$ a_1 + a_3 + a_5 + \cdots = \frac{1}{2}(f(1) - f(-1)) = \frac{1}{2}(3^n - 1).$$
\item Since we want the sum of every fourth term, we consider the
fourth roots of unity, that is, the complex numbers with $x^4 =
1.$ These are $\pm 1, \pm i.$ Now consider the equalities
$$\begin{array}{lcl}
f(1)   & = & a_0 + a_1  + a_2 + a_3 + a_4 + a_5 + a_6 + a_7 + a_8 + a_9 + \cdots \\
f(-1) & = & a_0 - a_1 + a_2 - a_3 + a_4 - a_5 + a_6 - a_7 + a_8 - a_9 + \cdots \\
f(i)  & = & a_0 + ia_1  - a_2  -  ia_3 + a_4 + ia_5 - a_6 - ia_7 + a_8 + ia_9 + \cdots \\
f(-i) & = & a_0 - ia_1  - a_2  +  ia_3 + a_4 - ia_5 - a_6 + ia_7 + a_8 - ia_9 + \cdots \\
\end{array}$$
Summing these four rows,
$$ f(1) + f(-1) + f(i) + f(-i) = 4a_0 + 4a_4 + 4a_8 + \cdots ,$$whence
$$a_0 + a_4 + a_8 + \cdots = \frac{1}{4}(f(1) + f(-1) + f(i) + f(-i)) = \frac{1}{4}(3^n + 1 + i^n + (-i)^n).$$
\item  Consider the equalities
$$\begin{array}{lcl}
f(1)   & = & a_0 + a_1  + a_2 + a_3 + a_4 + a_5 + a_6 + a_7 + a_8 + \cdots \\
-f(-1) & = & - a_0 + a_1 - a_2 + a_3 - a_4 + a_5 - a_6 + a_7 - a_8 +  \cdots \\
-if(i)  & = & -ia_0 + a_1  + ia_2  -  a_3 - ia_4  + a_5 + ia_6 - a_7  - ia_8 + \cdots \\
if(-i) & = & ia_0 + a_1  - ia_2  - a_3 + ia_4  + a_5 - ia_6  - a_7 + ia_8  +  \cdots \\
\end{array}$$
Adding
$$ f(1) - f(-1) - if(i) + if(i) = 4a_1 + 4a_5 + 4a_9 + \cdots , $$whence
$$ a_1 + a_5 + a_9 + \cdots  = \frac{1}{4}\left( 3^n - 1 - i^{n + 1}  - (-i)^{n + 1}\right) .$$
\end{enumerate}
\end{answerexer}
\end{exer}
\begin{exer} Find the exact numerical value of $$ \sum _{k = 0} ^{665} \binom{1995}{3k}.$$
\begin{answerexer} Since we want every third term starting with the zeroth
one, we consider the cube roots of unity, that is, $\omega ^3 =
1$. These are $\omega = -1/2 - \sqrt{3}/2, \omega ^2 = -1/2 +
\sqrt{3}/2$ and $\omega ^3 = 1.$ If $\omega \neq 1,$ then $1 +
\omega + \omega ^2 = 0.$ If $\omega = 1, 1 + \omega + \omega ^2 =
3.$ Thus if $k$ is not a multiple of 3, $1^k + \omega ^k + \omega
^{2k} = 0,$ and if $k$ is a multiple of 3, then $1^k + \omega ^k +
\omega ^{2k} = 3.$ By the Binomial Theorem we then have
$$ {\everymath{\displaystyle}\begin{array}{lcl}(1 + 1)^{1995} + (1 + \omega)^{1995}  & & \\
\quad +  (1 + \omega ^2)^{1995} & = &
\sum _{k \leq 1995} (1^k + \omega ^k + \omega ^{2k})\binom{1995}{k} \\
& = & \sum _{k \leq 665} 3\binom{1995}{3k}.
\end{array}}$$But $(1 + \omega)^{1995} = (-\omega ^2)^{1995} = -1,$ and $(1 + \omega ^2)^{1995}
= (-w)^{1995} = - 1$. Hence $$ \sum _{k \leq 665} \binom{1995}{3k}
= \frac{1}{3}(2^{1995} - 2).$$
\end{answerexer}
\end{exer}

\end{multicols}


\section{Iteration and Recursion}
\begin{df}
Given a function $f$, its {\em iterate at $x$} is $f(f(x))$, that is, we use its value as the new input.  The iterates at $x$
$$ x, \ f(x), \ f(f(x)), \ f(f(f(x))), \ldots  $$ are called {\em $0$-th iterate}, {\em $1$st iterate}, {\em $2$nd iterate}, {\em $3$rd iterate}, etc.
We denote the $n$-th iterate by $f^{[n]}$.
\end{df}
In some particular cases it is easy to find the $n$th iterate of a function, for example
$$a(x)=x^t\implies a^{[n]}(x) =x^{t^n}, $$
$$b(x)=mx\implies b^{[n]}(x) =m^nx, $$
$$c(x)=mx+k\implies c^{[n]}(x) =m^nx+k \left(\dfrac{m^n-1}{m-1}\right). $$
The above examples are more the exception than the rule. Even if its possible to find a closed formula for the $n$-th iterate
some cases prove quite truculent.
\begin{exa} Let
$f(x) = \dfrac{1}{1 - x}$. Find the $n$-th iterate of $f$ at $x$, and determine the set of values of $x$ for which it makes sense.
\end{exa}
\begin{solu} We have
$$f^{[2]}(x) = (f\circ f)(x) = f(f(x)) = \frac{1}{1 - \frac{1}{1 - x}} = \frac{x - 1}{x},  $$
$$f^{[3]}(x) = (f\circ f \circ f)(x) = f(f^{[2]}(x))) =
f\left(\frac{x - 1}{x}\right) = \frac{1}{1 - \frac{x - 1}{x}}
= x.  $$
Notice now that  $f^{[4]}(x) = (f\circ f^{[3]})(x) =
f(f^{[3]}(x)) = f(x) = f^{[1]}(x)$. We see that $f$ is cyclic of
period $3$, that is, $$f^{[1]}(x) = f^{[4]}(x) = f^{[7]}(x) = \ldots = \dfrac{1}{1-x}, $$  $$f^{[2]}(x)
= f^{[5]}(x) = f^{[8]}(x) = \ldots = \dfrac{x-1}{x}, $$  $$f^{[3]}(x) = f^{[6]}(x) = f^{[9]}(x) =
\ldots =x.$$
The formul\ae\ above hold for $x\not\in\{0,1\}$.
\end{solu}
If there are functions $\phi$ and $g$ for which \begin{equation}f\circ \phi = \phi \circ g,\label{eq:iterates}\end{equation} then $f=\phi \circ g \circ \phi ^{-1}$. If the iterates of $g$
are easy to find, then\footnote{The reader who has seen Linear Algebra will recognise that this is the same
idea involving powers of similar matrices.} \begin{equation}f^{[n]} = \phi \circ g^{[n]}\circ \phi ^{-1},\label{eq:iterate2}\end{equation}
provides the $n$th iterate of $f$.
\begin{exa}
Let $f(x)=2x^2-1$. Find $f^{[n]}(x)$.
\end{exa}
\begin{solu} Observe that since $2\cos^2y-1=\cos 2y$, we may take $\phi (x)=\cos x$ and $g(x)=2x$ in (\ref{eq:iterates}). Since $g^{[n]}(x) = 2^nx$,
by virtue of (\ref{eq:iterate2}),
$$ f^{[n]}(x) = \cos (2^n\arccos x). $$This formula is valid for $|x|\leq 1$.
\end{solu}
\begin{exa}
Let $f(x)=4x(1-x)$. Find $f^{[n]}(x)$.
\end{exa}
\begin{solu} Observe that since $$4\sin^2y-4\sin^4y=4\sin^2y(1-\sin^2y)=(2\sin y\cos y)^2=\sin^2 2y,$$ we may take $\phi (x)=\sin^2 x$ and $g(x)=2x$ in (\ref{eq:iterates}). Since $g^{[n]}(x) = 2^nx$,
by virtue of (\ref{eq:iterate2}),
$$ f^{[n]}(x) = \sin^2 (2^n\arcsin \sqrt{x}). $$This formula is valid for $0\leq x\leq 1$.
\end{solu}







\begin{df}
Let $c_0, c_2, \ldots , c_k$ be real constants and $f:\BBN \to \BBR$
a function. A recurrence relation of the form $$ c_0a_n +
c_1a_{n+1}+ c_2a_{n+2}+\cdots + + c_ka_{n+k}=f(n), \qquad n \geq 0.
$$is called a {\em linear difference equation}. If $f$ is
identically zero, we say that the equation is {\em homogeneous}.
\end{df}


We begin by examining some simple  recursions of first order.

\begin{exa} \label{exa:simplest-recursion}Let $x_0 = 7$ and $x_n = 2x_{n - 1}, n \geq 1.$ Find a closed form for $x_n.$\end{exa}
\begin{solu} We have
$$
\begin{array}{lcl}
x_0 & = & 7 \\
x_1 & = & 2x_0 \\
x_2 & = & 2x_1 \\
x_3 & = & 2x_2 \\
\vdots & \vdots & \vdots \\
x_n & = & 2x_{n-1} \\
\end{array}
$$
Multiplying both columns,
$$x_0x_1\cdots x_n = 7\cdot 2^nx_0x_1x_2\cdots x_{n - 1}.$$Cancelling the common factors on both sides of
the equality,
$$x_n = 7\cdot 2^n.$$
\end{solu}
\begin{exa} Let $x_0 = 7$ and $x_n = 2x_{n - 1} + 1, n \geq 1.$ Find a closed form for $x_n.$  \end{exa}
\begin{solu} We have:
$$
\begin{array}{lcl}
x_0 & = & 7 \\
x_1 & = & 2x_0  + 1\\
x_2 & = & 2x_1  + 1\\
x_3 & = & 2x_2  + 1\\
\vdots & \vdots & \vdots \\
x_{n - 1} & = & 2x_{n - 2} + 1 \\
x_n & = & 2x_{n-1} + 1\\
\end{array}
$$
Multiply the  $k$th row by $2^{n - k}$. We  obtain
$$
\begin{array}{lcl}
2^nx_0 & = & 2^n\cdot 7 \\
2^{n - 1}x_1 & = & 2^nx_0  + 2^{n - 1}\\
2^{n - 2}x_2 & = & 2^{n - 1}x_1  + 2^{n - 2}\\
2^{n - 3}x_3 & = & 2^{n - 2}x_2  + 2^{n - 3}\\
\vdots & \vdots & \vdots \\
2^2x_{n - 2} & = & 2^3x_{n - 3} + 2^2 \\
2x_{n - 1} & = & 2^2x_{n - 2} + 2 \\
x_n & = & 2x_{n-1} + 1\\
\end{array}
$$Adding both columns, cancelling, and adding the geometric sum,
$$x_n = 7\cdot 2^n + (1 + 2 + 2^2 + \cdots + 2^{n - 1}) = 7\cdot 2^n + 2^n - 1 = 2^{n + 3} - 1.$$


{\em Aliter:} Let $u_n = x_n + 1 = 2x_{n - 1} + 2 = 2(x_{n - 1} +
1) = 2u_{n - 1}.$  We solve the recursion $u_n = 2u_{n - 1}$ as we
did  example \ref{exa:simplest-recursion}: $u_n = 2^nu_0 = 2^n(x_0 + 1) = 2^n\cdot
8 = 2^{n + 3}. $ Finally, $x_n = u_n - 1 = 2^{n + 3} - 1.$
\end{solu}


\begin{exa}[Oval's on the Plane]\label{exa:ovals-recurrence} Let there be drawn $n$ ovals on the plane. If an oval intersects
each of the other ovals at exactly two points and no three ovals
intersect at the same point, find a recurrence relation for the
number of regions into which the plane is divided. \end{exa}
\begin{solu}  Let this number be $a_n$. Plainly $a_1 = 2$.  After
the $n - 1$th stage, the $n$th oval intersects the previous ovals at
$2(n - 1)$ points, i.e. the $n$th oval is divided into $2(n - 1)$
arcs. This adds $2(n - 1)$ regions to the $a_{n - 1}$ previously
existing. Thus
$$ a_n = a_{n - 1} + 2(n -1), \ a_1 = 2.$$ This is a non-homogeneous linear recurrence. To obtain a closed form,
write $$\begin{array}{lll}
 a_2 &  = &  a_1 + 2(1),\\
a_3 &  = &  a_2 + 2(2),\\
a_4 &  = &  a_3 + 2(3),\\
\vdots & \vdots & \vdots \\
  a_{n-1} &  = &  a_{n - 2} + 2(n -2),\\
  a_n &  = &  a_{n - 1} + 2(n -1),\\ \end{array}  $$
Add these equalities and cancel common terms on the left and right,
$$ a_2+a_3+a_4+\cdots + a_{n-1}+a_n = a_1+a_2+a_3+a_4+\cdots + a_{n-1} + 2(1+2+\cdots+ (n-1)) \implies a_n = a_1 + (n-1)n = n^2-n+2,  $$
upon using Corollary \ref{cor:sum-of-first-n-integ}.


\bigskip
A Maple sequence for solving this recurrence is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{ rsolve(\{a(k)=a(k-1)+2*(k-1), a(1)=2\}, a(k));}{}
\end{mapleinput}
\end{maplegroup}

\end{solu}







Suppose that $a_n=ar^n$, $r\neq 0$, is a solution to the homogeneous
differential equation
$$ c_0a_n +
c_1a_{n+1}+ c_2a_{n+2}+\cdots + + c_ka_{n+k}=0.
$$
Then
$$ c_0r^n+c_1r^{n+1}+c_2r^{n+2}+\cdots + +c_kr^{n+k}=0 \implies r^n(c_0+c_1r+c_2r^2+\cdots + +c_kr^k)=0 \implies c_0+c_1r+c_2r^2+\cdots + +c_kr^k=0. $$
The equation  $$c_0+c_1r+c_2r^2+\cdots + +c_kr^k=0$$ is called the
{\em characteristic equation} of the difference
equation.\footnote{In olden days these used to be called the {\em
secular equation}.} Clearly if $bs^n$, $s\neq r$, is a solution,
then $ar^n+bs^n$ is also a solution. This is the so-called {\em
superposition principle.}

\bigskip
We will not discuss here a general theory of how to solve difference
equations, we will only focus on some examples that will be used
later on. The interested reader may read \cite{Boo} for the more
general case. Let us, however, discuss the case of the second order
linear homogeneous difference equation
$$ c_0x^n+c_1x^{n+1}+c_2x^{n+2} = 0.$$
The characteristic equation is a quadratic equation, say
$$p(x):= c_0+c_1x+c_2x^2=0. $$This equation has two roots $r, s$, and so
$$ c_0+c_1x+c_2x^2=c_2(x-r)(x-s). $$ If $r\neq s$, then by the superposition principle we have seen
that $a_n =ar^n+bs^n$ for some constants $a, b$.  What happens if
$r=s$? In this case $r$ is a double root and $$p(x)=c_2(x-r)^2.
$$and also, $p'(x)=c_1+2c_2x=2c_2(x-r)$ Since $p'(r)=0$, we must have $c_1+2c_2r=0$.  Now, let  us try $nr^n$ as another solution to the difference equation. Then
$$c_0nr^n+c_1(n+1)r^{n+1}+c_2(n+2)r^{n+2}=0 \implies nr^n(c_0+c_1r+c_2r^2)+r^{n+1}(c_1+2c_2r) = nr^n0 +r^{n+1}0=0,  $$
whence $nr^n$ is also a solution.


\begin{exa}[Fibonacci Numbers] The {\em Fibonacci sequence} is given by $f_0=0$,
$f_1=1$, $f_2=1$, $f_3=2$, $f_4=3$, $f_5=5$, and in general,
$$ f_{n+1}=f_n+f_{n-1}, \qquad n \geq 1. $$
Find a closed formula for $f_n$.
\end{exa}
\begin{solu}
Suppose $ar^n$, $r\neq 0$, is a solution, then
$$ ar^{n+1}=ar^n+ar^{n-1} \implies ar^{n-1}(r^2-r-1)=0 \implies r = \dfrac{1\pm \sqrt{5}}{2}. $$
This means that
$$f_n=A\left(\dfrac{1+\sqrt{5}}{2}\right)^n +B\left(\dfrac{1-\sqrt{5}}{2}\right)^n,
$$for some constants $A$ and $B$ that we must determine. Now
$$ f_0 = 0 \implies 0 = A+B, \qquad f_1=1 \implies 1= A\left(\dfrac{1+\sqrt{5}}{2}\right) +B\left(\dfrac{1-\sqrt{5}}{2}\right).$$
Solving for $A$ and $B$ we find $A=\dfrac{1}{\sqrt{5}}=-B$. Hence
$$f_n =  \dfrac{1}{\sqrt{5}}\left(\dfrac{1+\sqrt{5}}{2}\right)^n -\dfrac{1}{\sqrt{5}}\left(\dfrac{1-\sqrt{5}}{2}\right)^n. $$
This closed form is called the {\em Cauchy-Binet Formula.} To obtain
the first $100$ Fibonacci numbers using Maple\TM use the following
commands. Notice that the double bars indicate Maple that it is
dealing with a sequence.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{f||0:=0; f||1:=1;}{}
\mapleinline{active}{1d}{ for n from 2 to 100 do f||n:=f||(n-1)+f||(n-2); od;}{}
\end{mapleinput}
\end{maplegroup}






Maple also has a command {\tt rsolve}, that solves recursions. Let
us use it to obtain the Cauchy-Binet formula. We have to change
slightly our notation because Maple reads $f||n$ differently from,
say, $f(n)$.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{ rsolve(\{f(k)=f(k-1)+f(k-2), f(0)=0, f(1)=1\}, f(n));      }{}
\end{mapleinput}
\end{maplegroup}
The answer that Maple displays appears to be different than the one
we obtained. Prove, by rationalising the denominator of
$\dfrac{1}{1\pm \sqrt{5}}$, that they are in fact equal.
\end{solu}

\begin{exa}
Find a closed formula for the  recursion $a_{n+2}= a_{n+1}+6a_n$,
$a_0=3$  and $a_1=1$.
\end{exa}

\begin{solu}
Suppose $ar^n$, $r\neq 0$, is a solution, then
$$ ar^{n+2}=ar^{n+1}+6ar^{n} \implies ar^n(r^2-r-6)=0 \implies r \in\{-3,2\}.
$$ Thus the solution must be of the form
$$ a_n =A(-3)^n+B2^n.$$Using the initial conditions,
$$ 3=a_0 =A+B, \qquad 1=a_1=-3A+2B \implies A=1, B=2. $$
Thus $$a_n=(-3)^n + 2^{n+1}.  $$
\end{solu}

\begin{exa}
Find a closed form for the recursion $a_{n+2}= 6a_{n+1}-9a_n$,
$a_0=2$  and $a_1=15$.
\end{exa}
\begin{solu}
Suppose $ar^n$, $r\neq 0$, is a solution, then
$$ ar^{n+2}=6ar^{n+1}-9ar^{n} \implies ar^n(r^2-6r+9)=0 \implies r =3,
$$a repeated root. Thus the solution must be of the form
$$ a_n =A3^n+Bn3^n.$$Using the initial conditions,
$$ 2=a_0 =A, \qquad 15=a_1=3A+3B \implies A=2, B=3. $$
Thus $$a_n=2\cdot 3^n + n3^{n+1}.  $$ To obtain the first $100$
terms of this sequence and to obtain a closed form for it use the
Maple\TM commands
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{  a||0:=2; a||1:=15;}{}
\mapleinline{active}{1d}{for n from 2 to 100 do a||n:=6*s||(n-1)-9*a||(n-2); od;}{}
\mapleinline{active}{1d}{  rsolve(\{a(k)=6*a(k-1)-9*a(k-2), a(0)=2, a(1)=15\}, a(n));}{}
\end{mapleinput}
\end{maplegroup}
\end{solu}

\begin{exa} Find the recurrence relation for the number of $n$ digit binary
sequences with no pair of consecutive $1$'s. \end{exa}  \begin{solu}
It is quite easy to see that $a_1 = 2, a_2 = 3.$ To form $a_n, n
\geq 3,$ we condition on the last digit. If it is 0, the number of
sequences sought is $a_{n - 1}$. If it is 1, the penultimate digit
must be 0, and the number of sequences sought is $a_{n - 2}$. Thus
$$ a_n = a_{n - 1} + a_{n - 2}, \qquad  a_1 = 2, \ a_2 = 3.$$
This recurrence looks like the Fibonacci recurrence. It is called
the {\em Lucas sequence.} We leave to the reader to prove that its
closed form is
$$ a_n =\left(\dfrac{1+2\sqrt{5}}{5}\right)\left(\dfrac{1+\sqrt{5}}{2}\right)^n +\left(\dfrac{1-2\sqrt{5}}{5}\right)\left(\dfrac{1-\sqrt{5}}{2}\right)^n.  $$
\end{solu}

\begin{exa}
Let $a_{n+2}-2a_{n+1}+2a_n=0$ with $a_0=1$ and $a_1=1$. Find a close form for this recursion.
\end{exa}
\begin{solu}
The characteristic equation is $r^2-2r+2=0 \implies r\in\{1-i, 1+i\}$. Hence
$$ a_n=A(1-i)^n+B(1+i)^n.$$Using the polar forms
$$1-i = \sqrt{2}e^{-\pi i/4}, \qquad 1+i = \sqrt{2}e^{\pi i/4}, $$ we may write
$$ a_n = C2^{n/2}\cos \dfrac{\pi n}{4} +D2^{n/2}\sin \dfrac{\pi n}{4}. $$
Now, $$a_0 = 1 \implies 1=C.$$
Also, $$a_1 = 1 \implies 1=C2^{1/2} \cos\dfrac{\pi}{4}+D2^{1/2} \sin\dfrac{\pi}{4} = C+D \implies D=0.  $$
The general solution is thus $a_n=2^{n/2}\cos \dfrac{\pi n}{4}$.
\end{solu}




\section*{\psframebox{Homework}}
\begin{multicols}{2}\columnseprule 1pt \columnsep 25pt\multicoltolerance=900
\begin{exer}
Let $f(x)=x^2-2$. Use the fact  that $\left(x+\dfrac{1}{x}\right)^2-2=x^2+\dfrac{1}{x^2}$
to prove that $$f^{[n]}(x) = \left(\dfrac{x+\sqrt{x^2-4}}{2}\right)^{2^n}+\left(\dfrac{x-\sqrt{x^2-4}}{2}\right)^{2^n} $$
for $|x|\geq 2$.
\end{exer}

\begin{exer}[Lines on the Plane] Find a recurrence relation for the number of regions into which
the plane is divided by $n$ straight lines if every pair of lines
intersect, but no three lines intersect.  \begin{answerexer} Let
$a_n$ be this number. Clearly $a_1 = 2.$ The $n$th line is cut by he
previous $n - 1$ lines at $n - 1$ points, adding $n$ new regions to
the previously existing $a_{n -1}.$ Hence $$ a_n = a_{n - 1} + n, \
a_1 = 2 .$$ We use the same method as in example
\ref{exa:ovals-recurrence} to solve this recurrence. write
$$\begin{array}{lll}
 a_2 &  = &  a_1 + 2,\\
a_3 &  = &  a_2 + 3,\\
a_4 &  = &  a_3 + 4,\\
\vdots & \vdots & \vdots \\
  a_{n-1} &  = &  a_{n - 2} + (n -1),\\
  a_n &  = &  a_{n - 1} + n,\\ \end{array}  $$
Add these equalities and cancel common terms on the left and right,
$$ a_2+a_3+a_4+\cdots + a_{n-1}+a_n = a_1+a_2+a_3+a_4+\cdots + a_{n-1} + (2+3+\cdots + n) \implies a_n = a_1 + \left(\dfrac{n(n+1)}{2}-1\right) =\dfrac{n^2+n+2}{2},  $$
upon using Corollary \ref{cor:sum-of-first-n-integ}. A Maple
sequence for solving this recurrence is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{rsolve(\{a(k)=a(k-1)+k, a(1)=2\}, a(n));}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}
\end{exer}

\begin{exer}
Solve the recursion $a_n=1+\sum _{k=1} ^{n-1} a_k$ for $n\geq 2$ and  $a_1=1$.
\begin{answerexer}
Observe that
$$a_n-a_{n-1} =\left(1+\sum _{k=1} ^{n-1} a_k\right)-\left(1+\sum _{k=1} ^{n-2} a_k\right)=a_{n-1}.   $$
This means that $a_n=2a_{n-1}$ and so
$$ \begin{array}{lll} a_n& = & 2a_{n-1}\\  a_{n-1}& = & 2a_{n-2}\\     \vdots & \vdots & \vdots \\  a_2& = & 2a_1\\ \end{array}$$
Multiplying all these equalities,
$$a_na_{n-1}\cdots a_2 = 2^{n-1}a_{n-1}a_{n-2}\cdots a_1 \implies a_n = 2^{n-1}a_1 = 2^{n-1}. $$

\end{answerexer}
\end{exer}


\begin{exer}
Let $x_0 = 1, x_n = 3x_{n - 1} - 2n^2 + 6n - 3$. Find a closed
form for this recursion.
 \begin{answerexer}$x_n = 3^n
+ n^2.$
\end{answerexer}
\end{exer}
\begin{exer}
Find a closed form for $x_{n} = 2x_{n - 1} + 3^{n - 1}, x_0 = 2.$
\begin{answerexer}  $x_n = 2^n + 3^n.$
\end{answerexer}
\end{exer}

\begin{exer}
Solve the recursion $a_n=2a_{n/2} + 6n-1$ for $n\geq 2$, $n$ a power of $2$, and  $a_1=1$.
\begin{answerexer}
Let $n=2, 2^2, \ldots 2^k$. Then
$$ \begin{array}{lll} a_2 & = & 2a_1 + 6(2)-1\\
a_4 & = & 2a_2 + 6(4)-1\\
a_8 & = & 2a_4 + 6(8)-1\\
\vdots & \vdots & \vdots \\
a_{2^{k-1}}&=&2a_{2^{k-2}} + 6(2^{k-1})-1\\
a_{2^k}&=&2a_{2^{k-1}} + 6(2^k)-1\\
   \end{array}$$
Multiplying successively each equation  by  $2^{k-1}, 2^{k-2}, \ldots , 2, 1$, obtaining,
$$ \begin{array}{lll} 2^{k-1}a_2 & = & 2^{k}a_1 + 6(2)\cdot 2^{k-1}-2^{k-1}\\
2^{k-2}a_4 & = & 2^{k-1}a_2 + 6(4)\cdot 2^{k-2}-2^{k-2}\\
2^{k-3}a_8 & = & 2^{k-2}a_4 + 6(8)\cdot 2^{k-3}-2^{k-3}\\
\vdots & \vdots & \vdots \\
2a_{2^{k-1}}&=& 2^2a_{2^{k-2}} + 6(2^{k-1})\cdot 2 -2\\
a_{2^k}& = & 2a_{2^{k-1}} + 6(2^k)-1\\
   \end{array}$$
Adding and cancelling,
$$a_{2^k}=2^ka_1+6k\cdot 2^k -(1+2+2^2+\cdots + 2^{k-1}) =2^k+ 6\cdot k\cdot 2^k - 2^k+1 = 6k2^k +1,   $$
where we have used Theorem \ref{thm:sum-finite-geom-series}. Now let $n\geq 1$ be an integer. If $2^k = n$ then $k=\log _2 n$ and
$$ a_n =6n(\log _2 n) +1. $$



\end{answerexer}
\end{exer}

\begin{exer}
Let $x_0 = 2, x_n = 9x_{n - 1} - 56n + 63$. Find a closed form for
this recursion.
\begin{answerexer} $x_n =
2(9^n) + 7n.$
\end{answerexer}
\end{exer}
\begin{exer} Let $x_0 = 7$ and $x_n = x_{n - 1} + n, n \geq 1.$ Find a closed formula for $x_n.$
\begin{answerexer} We have
$$
\begin{array}{lcl}
x_0 & = & 7 \\
x_1 & = & x_0 + 1 \\
x_2 & = & x_1  + 2\\
x_3 & = & x_2  + 3\\
\vdots & \vdots & \vdots \\
x_n & = & x_{n-1} + n \\
\end{array}
$$
Adding both columns,
$$x_0 + x_1 + x_2 + \cdots + x_n = 7 + x_0 + x_2 + \cdots + x_{n - 1} + (1 + 2 + 3 + \cdots + n).$$
Cancelling and using the fact that $\dis{1 + 2 + \cdots + n =
\frac{n(n + 1)}{2}}$,
$$x_n = 7 + \frac{n(n + 1)}{2}.$$
\end{answerexer}
\end{exer}

\begin{exer}
Solve the recursion $a_n=2a_{n-1} + n-1$ for $n\geq 2$ and  $a_1=1$.
\begin{answerexer}
Observe that
$$\begin{array}{lll} a_n & = & 2a_{n-1} + n-1\\
 a_{n-1} & = & 2a_{n-2} + n-2\\
a_{n-2} & = & 2a_{n-3} + n-3\\
\vdots & \vdots & \vdots \\
a_{3} & = & 2a_2 + 1\\
a_{2} & = & 2a_1 + 1\\
 \end{array} $$
Starting from the top, multiply successively by $2$, $2^2$, \ldots , $2^{n-1}$, obtaining,
$$\begin{array}{lll} 2a_n & = & 2^2a_{n-1} + 2(n-1)\\
 2^2a_{n-1} & = & 2^3a_{n-2} + 2^2(n-2)\\
2^3a_{n-2} & = & 2^4a_{n-3} + 2^3(n-3)\\
\vdots & \vdots & \vdots \\
2^{n-2}a_{3} & = & 2^{n-1}a_2 + 2^{n-2}\cdot 2\\
2^{n-1}a_{2} & = & 2^na_1 + 2^{n-1}\cdot 1\\
 \end{array} $$
 Adding and cancelling,
 $$ 2a_n =   2^na_1 + \sum _{k=1} ^{n-1} k2^{n-k} = 2^n + 2^n\sum _{k=1} ^{n-1} \dfrac{k}{2^k} =
 2^n +2^{n}\left(-\dfrac{2n}{2^n}-\dfrac{2}{2^n}+2\right)=3\cdot 2^n-2n-2,
  $$
 where we have used Corollary \ref{cor_derivative-geom-sum}.
 Finally,
 $$a_n=3\cdot 2^{n-1}-n-1.  $$
\end{answerexer}
\end{exer}

\begin{exer}[Putnam 1985] Let $d$ be a real number.  For each
integer $m \geq 0$, define a sequence ${a_m (j)}, j = 0, 1, 2,
\cdots$ by $a_m(0) = \frac{d}{2^m},$ and $a_m(j + 1) = (a_m(j +
1))^2 + 2a_m(j), j \geq 0.$ Evaluate
$$\lim _{n \rightarrow \infty} a_n(n).$$
\begin{answerexer}  Observe that $a_m(j + 1) + 1 = (a_m(j))^2 + 2a_m(j) + 1
= (a_m(j) + 1)^2.$ Put $v_j = a_m(j) + 1.$ Then $v_{j + 1} =
v^2_j,$ and $\ln v_{j + 1} = 2\ln v_j$; Put $y_j = \ln v_j .$ Then
$y_{j + 1} = 2y_j;$ and hence $2^ny_0 = y_n$ or $ 2^n \ln v_0 =
\ln v_n$ or $v_n = (v_0)^{2^n} = (1 + \frac{d}{2^m})^{2^n}$ or
$a_m(n) + 1 = (1 + \frac{d}{2^m})^{2^n}.$ Thus $a_n(n) =
(\frac{d}{2^n} + 1)^{2^n} - 1 \rightarrow e^d - 1$ as $n
\rightarrow \infty.$
\end{answerexer}
\end{exer}
\begin{exer}
A recursion satisfies $u_0 = 3, u_{n + 1} ^2 = u_{n}, n \geq 1.$
Find a closed form for this recursion.
\begin{answerexer}
 Let $v_n = \log u_n.$ Then $v_{n } = \log u_{n } = \log
u_{n - 1} ^{1/2} = \frac{1}{2} \log u_{n - 1} = \frac{v_{n -
1}}{2}.$ As $v_n = v_{n - 1}/2,$ we have $v_n = v_0/2^n$, that is,
$\log u_n = (\log u_0)/2^n$. Therefore, $u_n = 3^{1/2^n}.$
\end{answerexer}
\end{exer}

\begin{exer} There are two urns, one is full of water and the other is
empty. On the first stage, half of the contains of urn I is passed
into urn II. On the second stage 1/3 of the contains of urn II is
passed into urn I. On stage three, 1/4 of the contains of urn I is
passed into urn II. On stage four 1/5 of the contains of urn II is
passed into urn I, and so on. What fraction of water remains in urn
I after the 1978th stage?


\begin{answerexer} Let $x_n, y_n, n = 0, 1, 2, \ldots$ denote the fraction of
water in urns I and II respectively at stage $n$. Observe that $x_n
+ y_n = 1$ and that
\renewcommand{\arraystretch}{2}

$$
\begin{array}{ll}
x_0 = 1;  y_0 = 0 &  \\
& x_1 = x_0 - \frac{1}{2}x_0 = \frac{1}{2}; y_1 = y_1 + \frac{1}{2}x_0 = \frac{1}{2}\\
& x_2 = x_1 + \frac{1}{3}y_1 = \frac{2}{3}; y_2 = y_1 - \frac{1}{3}y_1 = \frac{1}{3} \\
& x_3 = x_2 - \frac{1}{4}x_2 = \frac{1}{2}; y_1 = y_1 + \frac{1}{4}x_2 = \frac{1}{2} \\
& x_4 = x_3 + \frac{1}{5}y_3 = \frac{3}{5}; y_1 = y_1 - \frac{1}{5}y_3 = \frac{2}{5} \\
& x_5 = x_4 - \frac{1}{6}x_4 = \frac{1}{2}; y_1 = y_1 + \frac{1}{6}x_4 = \frac{1}{2} \\
& x_6 = x_5 + \frac{1}{7}y_5 = \frac{4}{7}; y_1 = y_1 - \frac{1}{7}y_5 = \frac{3}{7} \\
& x_7 = x_6 - \frac{1}{8}x_6 = \frac{1}{2}; y_1 = y_1 + \frac{1}{8}x_6 = \frac{1}{2} \\
& x_8 = x_7 + \frac{1}{9}y_7 = \frac{5}{9}; y_1 = y_1 - \frac{1}{9}y_7 = \frac{4}{9} \\
\end{array}
$$
A pattern emerges (which may be proved by induction) that at each
odd stage $n$ we have $x_n = y_n = \frac{1}{2}$ and that at each
even stage we have (if $n = 2k$) $x_{2k} = \frac{k + 1}{2k + 1},
y_{2k} = \frac{k}{2k + 1}$. Since $\frac{1978}{2} = 989$ we have
$x_{1978} = \frac{990}{1979}$.
\end{answerexer}
\end{exer}


\begin{exer}[Towers of Hanoi]
The French mathematician Edouard Lucas furnished, in 1883, the toy seen in figure \ref{fig:towers-of-hanoi}  (with eight disks), along with the
following  legend. The tower of Brahma  had 64 disks of gold resting on three diamond needles. At the beginning of time, God placed these disks on the first needle
and ordained that a group of priests should transfer them to the third needle according to the following rules:
\begin{enumerate}
\item The disks are initially stacked on peg A, in decreasing order (from bottom to top).
\item The disks must be moved to another peg in such a way that only one disk is moved at a time and without stacking a larger disk onto a smaller disk.
\end{enumerate}
When they finish, the Tower will crumble and the world will end. Prove that if there are $n$ disks, then $2^n-1$ are necessary and sufficient to
perform the task according to the rules.
\end{exer}
\vspace{1cm}

\begin{figurehere}
\centering
\includegraphics[height=3.5cm,width=7cm]{tower-of-hanoi2.eps}
\vspace{1cm} \hangcaption{Towers of Hanoi.} 
\label{fig:towers-of-hanoi}
\end{figurehere}
\vspace{1cm}

\begin{exer}[Josephus' Problem] In \cite{HeKa} we find the following legend about the famous first-century Jewish historian
Flavius Josephus:
\begin{quote}
In the Jewish revolt against Rome, Josephus and 39 of his comrades were holding out against the Romans in a cave. With defeat imminent,
they resolved that, like the rebels at Masada, they would rather die than be slaves to the Romans.
They decided to arrange themselves in a circle. One man was designated as number one, and they proceeded clockwise killing every
seventh man\ldots . Josephus (according to the story) was among other things an accomplished mathematician; so he instantly
figured out where he ought to sit in order to be the last to go. But when the time came, instead of killing himself he joined the Roman side.
\end{quote}
In general, given a group of $n$ men arranged in a circle under the edict that every $m$th
man will be executed going around the circle until only one remains,
the object is to find the position $L(n,m)$ in which you should stand in order to be the last survivor.
The particular situation of Flavius Josephus is  asking for $L(40,7)$.
The general Josephus' Problem is very difficult.
Prove, however, that $L(n,2)=1+2n-2^{1+\floor{\log_2n}}$.
\end{exer}


\begin{exer}[Monkeys and Coconuts]
$N$ men and $M$ monkeys gather coconuts all day and then they fall asleep. The first man wakes up, separates $p$ coconuts for each monkey,
and then takes $\dfrac{1}{N}$
of what remains for himself and goes back to sleep. The second man wakes up,  separates $p$ coconuts for each monkey, and then takes $\dfrac{1}{N}$
of what remains for himself and goes back to sleep, etc. until the $N$th man wakes up and does the same. In the morning everyone wakes up, and the men give
$p$ coconuts to every monkey  and $\dfrac{1}{N}$ of what remains for themselves. Given that each division was an integer division, find the least amount
of coconuts needed.
\begin{answerexer}
Consider iterates of $f(x)=\dfrac{N-1}{N}(x-Mp),$ where $x$ is the initial amount of coconuts. Then
$x=tN^{N+1}-Mp(N-1)$, where
 $t$ is the smallest positive integer that makes $x$ positive.
\end{answerexer}
\end{exer}


\begin{exer}[Derangements] An absent-minded secretary is filling $n$
envelopes with $n$ letters. Find a recursion for the number $D_n$ of
ways in which she never stuffs the right letter into the right
envelope. \begin{answerexer} Number the envelopes $1, 2, 3, \cdots , n$.
We condition on the last envelope.  Two events might happen. Either
$n$ and $r (1 \leq r \leq n - 1)$ trade places or they do not.


\bigskip In the first case, the two letters $r$ and $n$ are
misplaced. Our task is just to misplace the other $n - 2$ letters,
$(1, 2, \cdots , r - 1, r + 1, \cdots , n - 1)$ in the slots $(1, 2,
\cdots , r - 1, r + 1, \cdots , n - 1).$ This can be done in $D_{n -
2}$ ways. Since $r$ can
be chosen in $n - 1$ ways, the first case can happen in $(n - 1)D_{n - 2}$ ways.\\


\bigskip

In the second case, let us say that letter $r$, $(1 \leq r \leq n -
1)$ moves to the $n$-th position but $n$ moves not to the $r$-th
position.  Since $r$ has been misplaced, we can just ignore it.
Since $n$ is not going to the $r$-th position, we may relabel $n$ as
$r$.  We now have $n - 1$ numbers to misplace, and this can be done
in
$D_{n - 1}$ ways.\\
As $r$ can be chosen in $n - 1$ ways, the total number of ways for
the second case is $(n - 1)D_{n - 1}.$  Thus $D_n = (n - 1)D_{n - 2}
+ (n - 1)D_{n - 1}.$
\end{answerexer}
\end{exer}
\end{multicols}



\chapter{Some Maple\TM Programming}

In this chapter we will introduce some algorithmic constructs:
looping, conditional expressions, etc. An {\em algorithm} is a set of vividly clear instructions that must be executed
in order to perform a well defined task. We will avail from the
software Maple\TM in order to illustrate these points. Maple\TM is
easy to use, and its basic syntax does not differ much from other
programming languages like Pascal, C, or Java.


\bigskip

Our object here will be to study the logic of writing small programs.  The topic of safeguarding our program against errors in inputs, and of
proving our algorithms correct, although important topics in computer programming,
will only distract us from our main goals, and hence we will not touched it here. Most the algorithms here will be numeric, it would be
a rare occurrence if we treat non-numeric algorithms.


\bigskip

Programming is a difficult subject for a beginner and it requires practice and attention to detail. Most of the exercises at the end of the section are solved.
I urge you to attempt them without looking at my solution. You should run each line through Maple.
Since these notes were hastily put together, the writing is somewhat cryptic.


\section{Basic Operations}
Although we now have versions past Maple IX, we will use the programming constructs of Maple IX in our
discussion. Our interest is to learn basic procedural programming and albeit the basic WYSIWYG constructs are easier for the
the oligophrenic, we will not make use of them here.

\bigskip



Maple uses $+$ for addition, $-$ for subtraction, $\wedge$
(circumflex accent) for exponentiation, $/$ for division, $!$ for
the factorial. The usual algebraic precedence of operators
(parentheses over exponents, over multiplication and division, over
addition and subtraction) is respected. Instructions are typed after the \verb+[>+ prompt, and must always be ended with a semicolon, after which
you must press the \fbox{ENTER} key. Whitespace is
ignored between characters.  If a colon is used instead of a semicolon, the command is executed silently, that is, Maple does not make visible the output.
To obtain a decimal approximation, either put a decimal point anywhere in the expression, or use the command {\tt evalf()}
(evaluate to floating point).
\begin{exa}Compute $\dfrac{1^1+2^2+3^3}{(4!+ 5\cdot 6 \cdot 7\cdot 8)^9}$.
using Maple.
\end{exa}
\begin{solu}
The required command line is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{(1^1+2^2+3^3)/(4!+ 5*6*7*8)^9;}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\dfrac {1}{3785091090811379105075822592}\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{evalf((1^1+2^2+3^3)/(4!+ 5*6*7*8)^9);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[.2641944344 \ 10^{-27}\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{(1^1+2^2+3^3)/(4!+ 5.*6*7*8)^9;}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[.2641944344 \ 10^{-27}\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}
The power of Maple rests on its ability to perform symbolic computations in a straightforward manner.
To operate with complex numbers, use the imaginary unit $I$ (capitalised). Maple is able to evaluate a large list of  common functions, among
them
\verb+sin()+, \verb+cos()+, \verb+tan()+, \verb+log()+, \verb+log[n]()+ \verb+exp()+, \verb+max()+, \verb+min()+, \verb+sqrt()+, \verb+abs()+,
\verb+floor()+, \verb+ceil()+. Enter $\pi$ as \verb+Pi+.

\begin{exa}
Evaluate the following using Maple.
$$ \sin \dfrac{\pi}{3} + \tan \dfrac{\pi}{6}, \quad (1+i)^{20}+(1-\sqrt{2})^{20}, \quad  \max (\floor{5.6}, \log 100).    $$
\end{exa}
\begin{solu}
The required command line is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{(sin(Pi/3)+tan(Pi/6);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\dfrac{5}{6}\sqrt{3}\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{(1+I)^20+(1-sqrt(2))^20;}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[-1024+ \left( 1-\sqrt {2} \right) ^{20}\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{(max(floor(5.6),log(100));}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[5\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}

Maple has several libraries that have tailor-made commands for Linear Algebra, Calculus, Plotting, Graph Theory, Number Theory, etc. Some combinatorial
and number theoretic functions of use are the following:
\begin{enumerate}
\item \verb+binomial(n,k)+ computes the binomial coefficient $\binom{n}{k}$
\item \verb+gcd(a,b)+ finds the greatest common divisor of the integers $a$ and $b$.
\item \verb+lcm(a,b)+ finds the least common multiple of the integers $a$ and $b$.
\item \verb+isprime(x)+ determines whether the integer $x$ is prime.
\item \verb+ithprime(k)+ gives the prime the $k$-th position, where $p_1=2$ is the first prime, $p_2=3$ is the second prime, etc.
\item \verb+nextprime(x)+ finds the prime just above  the integer $x$.
\item \verb+ifactor(x)+ gives the prime factorisation of the integer $x$.
\item \verb+iquo(a, b)+ finds the integral quotient when the integer  $a$ is divided by the integer $b$.
\item \verb+irem(a, b)+ finds the integral remainder when the integer  $a$ is divided by the integer $b$.
\item \verb+a mod b+ finds the integer  $a$ modulo the  integer $b$.
\end{enumerate}
\begin{exa}
Use Maple to find $\gcd \left(\binom{20}{10}, \binom{20}{15}\right)$.
\end{exa}
\begin{solu}
The required command line is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{gcd(binomial(20,10), binomial(20,15));}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[1292\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}
\begin{exa}
Use Maple to determine whether  $60637$\footnote{A well-known zip code\ldots } is prime.
Find the prime just above $60637$.
\end{exa}
\begin{solu}
The required command line is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{isprime(60637);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[true\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{nextprime(60637);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[60647\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}


Maple is able to operate symbolically. To multiply out an algebraic expression, use \verb+expand()+. To simplify
an expression, use \verb+simplify()+. This last command is rather limited and sometimes one needs to refine it, perhaps with the
the \verb+convert()+ command. The \verb+is+ command determines whether two formul\ae\ (involving numbers) are equal. To factor an expression use
the command \verb+factor()+.

\begin{exa}Multiply out $(a+b+c)(a^2+b^2+c^2-ab-bc-ca)$.
\end{exa}
\begin{solu}
The required command line is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{expand((a+b+c)*(a^2+b^2+c^2-a*b-b*c-c*a));}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[a^3+b^3+c^3-3abc\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}
\begin{exa}Factor $ x^{10}-x^8-2x^7-x^6-x^4+x^2+2x+1$ using Maple.
\end{exa}
\begin{solu}
The required command line is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{factor(x^10-x^8-2*x^7-x^6-x^4+x^2+2*x+1);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[( x-1)( x+1)(x^2-x+1)(x^2-x-1)(x^2+x+1)^2\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}
\begin{exa} Obtain the partial fraction expansion of $\dfrac{x}{x^3+1}$ using Maple.
\end{exa}
\begin{solu}
The required command line is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{convert(x/(x^3-1), parfrac,x);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\dfrac{1}{3}\cdot \dfrac{1}{x-1}-\dfrac{1}{3}\cdot \dfrac{x-1}{x^2+x+1}\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}
\begin{exa} Use Maple to find the exact value of $\cos \dfrac{\pi}{30}$.
\end{exa}
\begin{solu}
The required command line is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{convert(cos(Pi/30), radical);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\dfrac{1}{8}\sqrt {2}\sqrt {5-\sqrt {5}}+ \left(\dfrac{1}{8}+\dfrac{1}{8}\,\sqrt {5} \right)
\sqrt {3}
\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}
\begin{exa} Reduce the fraction $\dfrac{x-1}{x^4-1}$.
\end{exa}
\begin{solu}
The required command line is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{simplify((x-1)/(x^4-1));}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\dfrac{1}{x^3+x^2+x+1}\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}

Maple is able to differentiate and integrate functions symbolically with the commands \verb+diff()+ and \verb+int()+.
It is also able to add or multiply numbers in sequence with the commands \verb+sum()+ and \verb+product()+.

\begin{exa} Find a closed formula for the sum  $$\sum _{1\leq k \leq n}k^2 = 1^2 + 2^2+\cdots + n^2.  $$.
\end{exa}
\begin{solu}
The command line appears below. So that the formula appears in a familiar shape, we factor the result.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{factor(sum(k^2,k=1..n));}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\dfrac{1}{6}n(n+1)(2n+1)\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}
\begin{exa} Find the prime factorisation of the product   $$\prod _{1\leq k \leq 50} (2k-1) = (1)(3)(5)\cdots (99).  $$.
\end{exa}
\begin{solu}
The command line appears below.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{ifactor(product(2*k-1,k=1..50));}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[(3)^{26}(5)^{12}(7)^8(11)^5(13)^4(17)^3(19)^3(23)^2(29)^2(31)^2(37)(41)(43)(47)(53)(59)(61)(67)(71)(73)(79)(83)(89)(97)\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}
\begin{exa} Find  the derivative and the integral of the function $x\mapsto \dfrac{x}{x^3+1} $  with respect to $x$. Also, find the definite integral
$\int _{-1/2}^{1/2}  \dfrac{x\d{x}}{x^3+1}$.
\end{exa}
\begin{solu}
The command lines appear below.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{diff(x/(x^3+1),x);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\dfrac{1}{x^3+1}-\dfrac{3x^3}{(x^3+1)^2}\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{int(x/(x^3+1),x);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[-\dfrac{1}{3}\log (x+1)+\dfrac{1}{6}\log (x^2-x+1)+\dfrac{1}{3}\sqrt{3}\arctan \left(\dfrac{1}{3}(2x-1)\sqrt{3}\right)\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{int(x/(x^3+1),x=-1/2..1/2);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[-\dfrac{1}{6}\log 3-\dfrac{1}{6}\log 7+\dfrac{1}{3}\sqrt{3}\arctan \left(\dfrac{2}{3}\sqrt{3}\right)\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}






\section*{\psframebox{Homework}}
\begin{multicols}{2}\columnseprule 1pt \columnsep 25pt\multicoltolerance=900

\begin{exer}
Find $$(123456789)^2-(123456787)\cdot (123456791)$$ using Maple.
\begin{answerexer} The required sequence is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{(123456789)^2-(123456787)*(123456791);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[4\]}
\end{maplelatex}
\end{maplegroup}
\end{answerexer}
\end{exer}
\begin{exer}
Use Maple to verify that for any integers $a$ and $b$ it holds that $\gcd (a,b)\cdot \lcm{a, b}=ab$.
\begin{answerexer}
The required command line is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{gcd(a, b)*lcm(a,b);}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}
\end{exer}
\begin{exer} Compute
$$\frac{(10^4 + 324)(22^4 + 324)(34^4 + 324)(46^4 + 324)(58^4 + 324)}{(4^4 + 324)(16^4 + 324)(28^4 + 324)
(40^4 + 324)(52^4 + 324)}$$ using Maple. Then do this by hand.

\begin{answerexer}
The required sequence is

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{ ((10^4 + 324)*(22^4 + 324)\\ *(34^4 + 324)*(46^4 + 324)\\ *(58^4 + 324))/((4^4 + 324)\\ *(16^4 + 324)*(28^4 + 324)\\ *(40^4 + 324)*(52^4 + 324));\\   }}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[373\]}
\end{maplelatex}
\end{maplegroup}

Using Sophie Germain's trick,
$$a^4+4b^4=a^4+4a^2b^2+4b^4=(a^2+2b^2)^2-(2ab)^2=(a^2-2ab+2b^2)(a^2+2ab+2b^2),$$ and so with $b=3^4$, we gather that
$$ a^4+324=(a(a+6)-18)(a(a-6)+18),$$meaning that most factors cancel out, leaving just
$$\dfrac{58\cdot 64 + 18}{-2\cdot 4 + 18} = \dfrac{3730}{10}=373.$$
\end{answerexer}
\end{exer}
\begin{exer}
Find $\dint \dfrac{\d{x}}{\sqrt{1+\sqrt{1+\sqrt{x}}}}$ both by hand
and using Maple.
\begin{answerexer}
Put $u =\sqrt{1+\sqrt{1+\sqrt{x}}}$, then $x=(u^2-2)^2u^4$ and
$\d{x} = (4u^3(u^2-2)^2+4u^5(u^2-2))\d{u}$. Hence
$$\begin{array}{lll} \dint \dfrac{\d{x}}{\sqrt{1+\sqrt{1+\sqrt{x}}}} & = & \dint \dfrac{(4u^3(u^2-2)^2+4u^5(u^2-2))\d{u}}{u} \\
& = & 4\dint u^2(u^2-2)^2\d{u} +4\dint
u^4(u^2-2)\d{u}\\
& = & 4\dint (u^6-4u^4+4u^2)\d{u} +4\dint (u^6-2u^4)\d{u}\\
& = & 8\dint u^6\d{u}-24\dint u^4\d{u} + 16\dint u^2\d{u}\\
& = & \dfrac{8}{7}u^7 - \dfrac{24}{5}u^5+\dfrac{16}{3}u^3+C \\
& = & \dfrac{8}{7}(\sqrt{1+\sqrt{1+\sqrt{x}}})^7 - \dfrac{24}{5}(\sqrt{1+\sqrt{1+\sqrt{x}}})^5+\dfrac{16}{3}(\sqrt{1+\sqrt{1+\sqrt{x}}})^3+C. \\
\end{array}$$
The required command line is
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{int(1/sqrt(1+sqrt(1+sqrt(x))),x);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[1/2\,\sqrt {2}x{\it hypergeom} \left( [2,1/4,3/4],[3,3/2],-\sqrt {x}
 \right)
\]}
\end{maplelatex}
\end{maplegroup}
Note: Maple X expresses the answer in terms of hypergeometric
functions, and hence, our solution is perhaps better.
\end{answerexer}
\end{exer}
\begin{exer} Evaluate the definite integral  $\int _{-1} ^2 \max (|x-1|, x^2+2)\d{x}$.
\begin{answerexer}
The command lines appear below.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{int(max(abs(x-1),x^2+2),x=-1..2);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[9\]}
\end{maplelatex}
\end{maplegroup}
\end{answerexer}
\end{exer}

\begin{exer}Find $\dint \sqrt{\tan x} \ \d{x} $ both by hand and using
Maple.
\begin{answerexer}
Put $u=\sqrt{\tan x}$ and so $u^2 = \tan x$, $2u\d{u} = \sec^2x\d{x}
= (\tan^2x+1)\d{x} = (u^4+1)\d{x}$. Hence the integral becomes
$$\dint \sqrt{\tan x} \d{x} = 2\dint \dfrac{u^2}{u^4+1}\d{u}.  $$To
decompose  the above fraction into partial fractions observe (Sophie
Germain's trick) that $u^4+1 = u^4+2u^2+1 -2u^2=
(u^2+u\sqrt{2}+1)(u^2-u\sqrt{2}+1)$ and hence
$$\begin{array}{lll} \dint \sqrt{\tan x} \d{x} & = & 2\dint \dfrac{u^2}{u^4+1}\d{u} \\
& = & -\dfrac{\sqrt{2}}{2}\dint \dfrac{u}{u^2+u\sqrt{2}+1}\d{u}+
\dfrac{\sqrt{2}}{2}\dint \dfrac{u}{u^2-u\sqrt{2}+1}\d{u}\\
& = & -\dfrac{\sqrt{2}}{4}\log  (u^2+u\sqrt{2}+1) +
\dfrac{\sqrt{2}}{4}\log  (u^2-u\sqrt{2}+1) +
\dfrac{\sqrt{2}}{2}\arctan (\sqrt{2}u+1)-\dfrac{\sqrt{2}}{2}\arctan
(-\sqrt{2}u+1)+C\\
& = &-\dfrac{\sqrt{2}}{4}\log  (\tan x+\sqrt{2\tan x}+1) +
\dfrac{\sqrt{2}}{4}\log  (\tan x-\sqrt{2\tan x}+1) \\ & & \qquad  +
\dfrac{\sqrt{2}}{2}\arctan (\sqrt{2\tan
x}+1)-\dfrac{\sqrt{2}}{2}\arctan (-\sqrt{2\tan x}+1)+C
\end{array}$$


The required Maple sequence is

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{int(sqrt(tan(x)),x);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[1/2\,{\frac {\sqrt {2}\sqrt {\tan \left( x \right) }\cos \left( x
 \right) \arccos \left( \cos \left( x \right) -\sin \left( x \right)
 \right) }{\sqrt {\cos \left( x \right) \sin \left( x \right) }}}-1/2
\,\sqrt {2}\ln  \left( \cos \left( x \right) +\sqrt {2}\sqrt {\tan
 \left( x \right) }\cos \left( x \right) +\sin \left( x \right)
 \right)
\]}
\end{maplelatex}
\end{maplegroup}
\end{answerexer}
\end{exer}


\begin{exer} Compute
$$\dfrac{(1+i)^{2004}}{(1-i)^{2000}}$$ using Maple.
\begin{answerexer}
The required sequence is


\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{(1+I)^2004/(1-I)^2000;}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[-4\]}
\end{maplelatex}
\end{maplegroup}
\end{answerexer}
\end{exer}

\begin{exer} Factor  $1002004008016032$ using Maple.
\begin{answerexer}
The command line is

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{ifactor(1002004008016032);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[(2)^5(3)^2(7)(109)^2(167)(250501)\]}
\end{maplelatex}
\end{maplegroup}


\end{answerexer}
\end{exer}
\begin{exer}
Use Maple to verify that
$$(x + y)^5 - x^5 - y^5 = 5xy(x + y)(x^2 + xy + y^2)$$
and
$$(x + a)^7 - x^7 - a^7 = 7xa(x + a)(x^2 + xa + a^2)^2.$$
\begin{answerexer}
The required command lines are

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{factor((x + y)^5 - x^5 - y^5);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[5xy(x+y)(y^2+xy+x^2)\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{factor((x + y)^7 - x^7 - y^7);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[7xy(x+y)(y^2+xy+x^2)^2\]}
\end{maplelatex}
\end{maplegroup}
\end{answerexer}
\end{exer}
\begin{exer}
Write Maple code to verify that a product of sums of squares can be
written as a sum of squares, that is, verify that $$(a^2 + b^2)(c^2
+ d^2) = (ac + bd)^2 + (ad - bc)^2.$$
\begin{answerexer}
Here is one possible answer

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{is((a^2 + b^2)*(c^2+ d^2)= (a*c + b*d)^2 + (a*d - b*c)^2);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[true\]}
\end{maplelatex}
\end{maplegroup}
\end{answerexer}
\end{exer}
\begin{exer}Let $i^2=-1$.
Evaluate $$1+2i+3i^2+4i^3+5i^4+\cdots + 2007i^{2006}$$using Maple.
\begin{answerexer}
Here is one possible answer

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{ sum(k*I^(k-1), k=1..2007);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[-1004 + 1004 I\]}
\end{maplelatex}
\end{maplegroup}
\end{answerexer}
\end{exer}
\begin{exer}
Give Maple code to compute $\sum _{k = 1} ^{1000} \floor{\log_2 k}$.

\begin{answerexer} Here is a possible way.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{simplify(sum(floor(log[2](k)),k=1..1000));}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[7987\]}
\end{maplelatex}
\end{maplegroup}
\end{answerexer}
\end{exer}

\begin{exer}\label{exa:valor-exacto-cos-72}Find the exact value of
 $\cos \dfrac{\pi}{5}$ using Maple and by hand.

\begin{answerexer}The following Maple routine finds the exact value.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{ convert(cos(Pi/5),radical);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\dfrac{1}{4}\sqrt{5}+\dfrac{1}{4}\]}
\end{maplelatex}
\end{maplegroup}



Consider a regular pentagon $ABCDE$. Let  $x$ be the length of any one of its sides.
Recall that the Golden Section  $ \tau$  satisfies
$$ \tau > 0, \quad \dfrac{1}{\tau} = \dfrac{\tau}{1+\tau} \implies \tau =  \dfrac{1+\sqrt{5}}{2}.$$

\vspace{2cm}

\begin{figure}[!hp]
 \centering \psset{unit=3.5pc}
\pstGeonode[PosAngle={0,90,180,-90,-90}](1;18){A}(1;90){B}(1;162){C}(1;234){D}(1;306){E}
\pstLineAB{A}{B}\pstLineAB{B}{C}\pstLineAB{C}{D}\pstLineAB{D}{E}\pstLineAB{E}{A}
\pstLineAB[linestyle=dotted]{B}{E}\pstLineAB[linestyle=dotted]{C}{E}\pstLineAB[linestyle=dashed]{A}{C}
\pstInterLL[PosAngle={225}]{A}{C}{E}{B}{F}
\pstProjection[PointSymbol=none,PointName=none]{A}{B}{F}[F']
\pstRightAngle[RightAngleSize=.15]{F}{F'}{A} \pstLineAB{F}{F'}
\vspace{2cm}\footnotesize\hangcaption{Problem
\ref{exa:valor-exacto-cos-72}.}
\end{figure}


Let  $F$ be the point of intersection of the line segment   $\seg{AC}$ and
$\seg{BE}$. Since $\seg{AC}\parallel\seg{DE}$ , $\ang{FCE} = \ang{
CED}$ and thus  $\triangle FCD\equiv \triangle DEC$. Hence $FC=CD
= x$. Observe that   $\triangle FAB$ is isosceles and similar to $\triangle FCE$.  Letting $t=AF$
and observing that   $CE = CA = x+t$,
we have,
$$ \dfrac{FA}{FC} = \dfrac{BA}{CE} \implies \dfrac{t}{x} = \dfrac{x}{t+x} \implies \dfrac{1}{\frac{x}{t}} = \dfrac{\frac{x}{t}}{1+\frac{x}{t}} \implies \dfrac{x}{t} = \tau. $$
Since   $\ang{FCE} = \ang{CED}$ and $\ang{BCA} = \ang{
FCE}$, we have  $\ang{BCA} = \ang{FCE}=\ang{CED} =
\dfrac{1}{3}\cdot \dfrac{3\pi}{5}= \dfrac{\pi}{5}$. This means that
  $\ang{FCE} = \dfrac{3\pi}{5}$ and hence  $\ang{ABF} =
\ang{FAB} = \dfrac{\pi}{5}$. Erecting a perpendicular from $F$
to $\seg{AB}$,  we deduce from  $\triangle FAB$,
$$  \cos \dfrac{\pi}{5} = \dfrac{\frac{x}{2}}{t} = \dfrac{x}{2t} = \dfrac{\tau}{2} = \dfrac{1+\sqrt{5}}{4}.  $$

\end{answerexer}
\end{exer}
\end{multicols}
\section{Sets, Lists, and Arrays}
Maple has a rich variety of data structures, among them sets, lists, and arrays. Roughly speaking,  a {\em set} corresponds to a set in combinatorics:
the order of the elements is irrelevant, and repetitions are not taken into account. Sets are defined by using curly braces $\{\quad \}$.
In a {\em list}, the order of the elements is important and repetitions are taken into account.  Lists are defined by using square brackets
$[\quad ]$. Arrays are a generalisations of matrices. They can be modified and are declared with the command \verb+array()+.



We will first consider sets and set operations. In order to facilitate our presentation, we will give names to the various objects we will define. In order
to attach a name, we need the assignment operator \verb+:=+, where there is no space between the colon and the equal sign. Maple is able to perform set operations
with the commands \verb+union+, \verb+intersect+, and \verb+minus+. To check whether two sets are equal we may use the command \verb+evalb()+ (evaluate boolean).

\begin{exa}
Consider the sets
$$ A=\{1,2,3,a,b,c,d\}, \quad B=\{3,4,5,a,b,e,f\}. $$
Use Maple to obtain $$ A\cup B, \quad A \cap B,\quad A \setminus B,
$$and to verify that $$(A\setminus B)\cup (B\setminus A) = (A\cup B)\setminus (A\cap B).$$
\end{exa}
\begin{solu}
We first define the sets and then perform the desired operations. The following command lines accomplish what is required.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{A:=\{1,2,3,a,b,c,d\};}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[A:=\left\{ 1,2,3,a,b,c,d \right\}\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{B:=\{3,4,5,a,b,e,f\};}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[B:=\{3,4,5,a,b,e,f\}\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{A union B}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\{1, 2, 3, 4, 5, f, a, b, c, d, e\}\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{A intersect B;}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\{3, a, b\}\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{A minus B;}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\{1, 2, c, d\}\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{evalb((A union B) minus (A intersect B)=(A minus B) union (B minus A));}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[true\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}
We do not need to write code {\em in extenso} in order to define a set whose elements are in sequence, as we may use the the function
\verb+seq()+.
\begin{exa}
To define the set
$$X:=\{1,2,\ldots , 100\}  $$we type

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{X:=\{seq(k,k=1..100)\};}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\begin{array}{l}X := \{1, 2, 3, 4, 5, 6, 7, 8, 9, 10,\\
 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,\\ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,\\
  31, 32, 33, 34, 35, 36, 37, 38, 39, 40,\\ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,\\
   51, 52, 53, 54, 55, 56, 57, 58, 59, 60,\\ 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,\\
71, 72, 73, 74, 75, 76, 77, 78, 79, 80,\\ 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,\\
 91, 92, 93, 94, 95, 96, 97, 98, 99, 100\}\end{array}\] }
\end{maplelatex}
\end{maplegroup}
\end{exa}





A list is more or less like a set, except that repetitions are
allowed and the order of the elements is respected.
The function \verb+nops()+ gives the number of elements of the list.
To obtain the $i$-th element of the list, we type \verb+name[i]+,
enclosed in square brackets. We may also access an element of the
list by using the function \verb+op(i, name)+. We may also obtain elements in a range using these operators, for example
\verb+name[low..high]+ or, equivalently, \verb+op(low..high,name)+. The command \verb+subop(index1=newvalue1,index2=newvalue2,...,name)+.

\begin{exa}In this problem we perform various operations with lists.
\begin{enumerate}
\item Create the list $L1$ consisting of the elements $4$, $4$, $5$, $5$, $2$, $3$, $2$ in that order.
\item Create  the list
$L2$ consisting of the elements $a$, $b$, $b$, $c$, $c$, $a$, $d$ in that order.
\item Concatenate $L1$ and $L2$ into a list $L3$. \item Create a list
$L4$ consisting of the first three elements of $L1$ and the last three elements of $L2$.
\item Delete the first and last elements of $L1$ and substitute, respectively, with the values $x$ and $y$. Shew that $L1$ has not changed.
\end{enumerate}
\end{exa}
\begin{solu}The required commands follow.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{L1:=[4,4,5,5,2,3,2];}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[L1:=[4,4,5,5,2,3,2]\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{L2:=[a,b,b,c,c,a,d];}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[L2:=[a,b,b,c,c,a,d]\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{L3:=[op(L1),op(L2)];}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[L3 := [4, 4, 5, 5, 2, 3, 2, a, b, b, c, c, a, d]\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{L4:=[op(1..3,L1),op(-3..-1,L2)];}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[L4 := [4, 4, 5, c, a, d] \]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{subsop(1=x,-1=y,L1);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[[x, 4, 5, 5, 2, 3, y]\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{L1;}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[[4, 4, 5, 5, 2, 3, 2]\]}
\end{maplelatex}
\end{maplegroup}
\end{solu}

Before discussing arrays let us mention in passing a curious feature of Maple. Given a set  or a list  ($\{\ldots \}$ or $[\ldots ]$), we can  retrieve the members
 by appending $[\ ]$ at the end. Some examples follow.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\{2,3,4\}[\ ];}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[2,3,4\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{max(\{2,3,4\}[ ]);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[4\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{[1,2,3,4,5][\ ];}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[1,2,3,4,5\]}
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{min([1,2,3,4,5][\ ]);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[1\]}
\end{maplelatex}
\end{maplegroup}





An {\em array} is a more general data structure than a list, in fact, arrays are generalisations of the matrix concept.
Arrays are modifiable. Arrays are defined as \verb+array(index_function, ranges, initial_value_lists)+. All these parameters are optional.
The {\em dimension} of an array is the number
of indices used to describe it. A one-dimensional array is akin of a vector, and a two dimensional array is akin of a matrix.
Thus the one-dimensional array $x$ with $n$ elements essentially looks like
$$ x:=[x[1], x[2], \ldots , x[n]] $$
and a two-dimensional array $A$ with $mn$ elements (with $m$ rows and $n$-columns), essentially looks like
$$ A:=\begin{bmatrix} A[1,1] & A[1, 2] & A[1,3] & \cdots & A[1,n] \\
A[2,1] & A[2,2] & A[2,3] & \cdots & A[2,n] \\
\vdots & \vdots & \vdots & \cdots & \vdots \\
A[m,1] & A[m,2] & A[m,3] & \cdots & A[m,n] \\
\end{bmatrix} $$
In order to list the elements of an array we must use the command \verb+eval()+.
\begin{exa}
Define the one-dimensional array  $V:=[5,6,7,8,9]$. Then, change its third element to be $x$.
\end{exa}
\begin{solu}
The required code follows.


\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{V:=array([5,6,7,8,9]);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[V := [5, 6, 7, 8, 9]\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{eval(V);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[[5, 6, 7, 8, 9]\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{V[3]:=x;}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[V_3:=x\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{eval(V);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[[5, 6, x, 8, 9]\] }
\end{maplelatex}
\end{maplegroup}
\end{solu}
\begin{exa}
Define a $2\times 3$ array $M$ with $M_{11}=1$, $M_{12}=2$, $M_{13}=3$, $M_{21}=a$, $M_{22}=b$, and $M_{23}=c$.
 Then, redefine $M_{22}$ to be $x$. Also, define an uninitialised $3\times 2$ array $N$.
\end{exa}
\begin{solu}

The required code follows.


\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{M:=array([ [1,2,3],[a,b,c] ]);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[M:=\begin{bmatrix} 1 & 2 & 3 \cr a & b & c \cr\end{bmatrix}\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{M[2,2]:=x;}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[M_{2,2}:=x\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{eval(M);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\begin{bmatrix} 1 & 2 & 3 \cr a & x & c \cr\end{bmatrix}\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{N:=array(1..3,1..2);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[N := array(1 .. 3, 1 .. 2, [])\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{eval(N);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\begin{bmatrix} ?_{11} & ?_{12}  \cr ?_{21} & ?_{22} \cr ?_{31} & ?_{32} \cr\end{bmatrix}\] }
\end{maplelatex}
\end{maplegroup}

\end{solu}


\section*{\psframebox{Homework}}
\begin{multicols}{2}\columnseprule 1pt \columnsep 25pt\multicoltolerance=900
\begin{exer}
Consider the set of $100$ integers $X:=\{1,2,\ldots , 100\}$. Using Maple set operations, and Inclusion-Exclusion, find
the number of elements of $X$ which are neither multiples of $2$ nor $3$. Also, list all such elements.
\begin{answerexer}

Let $A:=\{2,4,6,\ldots, 100\}$, $B:=\{3, 6, 9, \ldots , 99\}$. We want the number of elements in $X\setminus (A\cup B)$.
The following Maple code calculates this. We have suppressed the outputs in order to economise space.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{X:=\{seq(k, k=1..100)\};}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{A:=\{seq(2*k,k=1..50)\};)}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{B:=\{seq(3*k,k=1..33)\};)}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{nops(X minus (A union B));}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{X minus (A union B);}{}
\end{mapleinput}
\end{maplegroup}

\end{answerexer}
\end{exer}
\begin{exer}
Consider the set of $1000$ integers $X:=\{1,2,\ldots , 1000\}$. Using Maple set operations, and Inclusion-Exclusion, find
the number of elements of $X$ which are neither perfect squares, nor perfect cubes, nor perfect fifth-powers. Also, list all such elements.
\begin{answerexer}
Let $A:=\{1^2,2^2,3^2,\ldots, 31^2\}$ (observe $\floor{\sqrt{1000}}=31$), $B:=\{1^3, 2^3,  \ldots , 10^3\}$ (observe $\floor{\sqrt[3]{1000}}=10$), and
$C:=\{1^5, 2^5, 3^5\}$ (observe $\floor{\sqrt[5]{1000}}=3$). We want the number of elements in $X\setminus (A\cup B\cup C)$.
The following Maple code calculates this. We have suppressed the outputs in order to economise space.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{X:=\{seq(k, k=1..1000)\};}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{A:=\{seq(k^2,k=1..31)\};)}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{B:=\{seq(k^3,k=1..10)\};)}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{C:=\{seq(k^5,k=1..3)\};)}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{nops(X minus (A union B union C));}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{X minus (A union B union C);}{}
\end{mapleinput}
\end{maplegroup}

\end{answerexer}
\end{exer}
\begin{exer}
Write Maple code that will compute the sum of the elements of a list of numbers.
\begin{answerexer}
Here is a possible answer. The code will not do anything unless a list $X$ is declared prior to it.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{sum(X[k], k=1..nops(X));}{}
\end{mapleinput}
\end{maplegroup}

\end{answerexer}

\end{exer}
\end{multicols}

\section{Functions and Procedures}
Recall that in mathematics we call a {\em function}
the collection of the following ingredients:
\begin{enumerate}
\item A set of inputs, called the {\em domain} of the function.
\item A set of {\em possible} outputs called the {\em target set} of the function.
\item A name for the function.
\item A name for a typical input (input parameter) of the function.
\item An {\em assignment rule} that assigns to every input a unique output.
\end{enumerate}
The collection of all the above ingredients is summarised in the notation
$$\fun{f}{x}{f(x)}{A}{B}  $$
where $f$ is the name of the function, $A$ is the domain, $B$ is the target set, $x$ is the typical input, and $f(x)$


\bigskip

Such definition of a function is especially useful in Computer Science. For example, if we had a function $f:\BBZ^3\to \BBR  $, we would write this in
C code has \verb+float f(int, int, int){instructions}+, where {\em float} refers to a floating (decimal) real number, and {\em int} refers to integer. This allocates
sufficient space in the memory to handle the inputs and outputs. Since memory is limited, we need to know before hand how much of it to allocate. In most of
your Precalculus and Calculus courses you have been misinformed when calling functions simply by their name and the assignment rule. This is unfortunate, because
say talking of the ``function'' $f$ with $f(x)=3x+1$ does not tell you anything about its domain and hence nothing about its image. It is also unfortunate because
you cannot tell whether the given function is injective, surjective, etc., simply by its assignment rule. On the other hand, this simplifies
matters when defining functions in Maple, we will simply have to be {\em very careful} that we input the right kind of inputs in our functions. Assigning the wrong kind of input to
a function where no provisions have been done for type-checking can lead us to infinite loops or program crashes. Since the programs we will write here are very simple,
we will not engage in this kind of safeguarding, but again, we insist that much care must be taken by the serious programmer to guard against possible
confusion and wrong inputs by the user.

\bigskip

There are at least two ways of defining functions in Maple. The ways we will explore are not completely equivalent and one has advantages over the other, but
we will not fuss with this now.

\bigskip

 The first way we will explore is through the arrow notation \verb+->+, which is obtained by a
dash and a greater than sign, with no spaces in between. This is reminiscent  with the way functions are defined in Precalculus and Calculus.
To name the function $f$ with $f(x)=3x+1$ you type \verb|f:=x->3*x+1|. The sum, difference, product and quotient of functions is obtained in the
expected way. To obtain $f\circ g$ we type \verb+f@g+. To obtain the output in a specific set or list we use the command \verb+map(x->f(x),X)+, where
$X$ is the name of the set or list.
\begin{exa} Consider the assignment rules $f(x)=x^2-x$ and $g(x)=2x+1$. Write Maple code
\begin{enumerate}
\item Defining both $f$ and $g$.
\item Computing $(f+g-fg)(2)$.
\item Computing the set $f(A)$, where $A$ is the set $A=\{-3,-2,-1,0,1,2,3\}$.
\item Computing the set $f(L)$, where $L$ is the list $L=[-3,-2,-1,0,1,2,3]$.
\item Computing $(f\circ g)(2)$.
\item Computing $\underbrace{(f\circ f \circ \cdots \circ f)}_{20 \
f\mathrm{'s}}(3)$.
\end{enumerate}
\end{exa}
\begin{solu}
The required commands appear below. Notice that since $f$ is not injective on the set $A$, there are fewer elements in $f(A)$ than in $A$.



\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{f:=x->x^2-x;}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[f := x\to x^2-x\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{g:=x->2*x+1;}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[g:=x\to 2x+1\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{(f+g-f*g)(2);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[-3\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{A:=\{-3,-2,-1,0,1,2,3\};}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[A:=\{-3,-2,-1,0,1,2,3\}\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{map(x->f(x), A);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\{0, 2, 6, 12\}\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{L:=[-3,-2,-1,0,1,2,3];}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[L:=[-3,-2,-1,0,1,2,3];\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{map(x->f(x), L);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[[12, 6, 2, 0, 0, 2, 6]\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{(f@g)(2);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[20\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{(f@20)(3);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[380\] }
\end{maplelatex}
\end{maplegroup}
\end{solu}

\begin{exa}
Write a function that takes a list of numbers as an input and outputs the average of the elements of the list.
Test the function with the list $[-1,2,3,3,4]$.
\end{exa}
\begin{solu}Here is a possible way.


\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{AVERAGE:=X-> sum(X[i],i=1..nops(X))/nops(X);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[AVERAGE:=X\to \dfrac{\sum _{i=1} ^{nops(X)}X_i}{nops(X)}\] }
\end{maplelatex}
\begin{mapleinput}
\mapleinline{active}{1d}{AVERAGE([-1,2,3,3,4]);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\dfrac{11}{5}\] }
\end{maplelatex}
\end{maplegroup}
\end{solu}
For our next example we need the command \verb+coeffs(p,x)+. This returns the set of coefficients of the polynomial $p$ in the variable $x$.
For example, the call \verb|coeffs(10*x^2-4*x+1,x)| returns \verb+{10,-4,1}+.

\begin{exa}The {\em height} of a polynomial $p(x)$ is the largest value of the absolute
values of its coefficients. Create a function
\verb+HEIGHT+ that finds the height of a given polynomial.
\end{exa}

\begin{solu}
Here is a possible answer. The idea is the following. The input is a polynomial $p$ in the indeterminate  $x$. \verb+map(abs, {coeffs(p,x)})+ creates a
set with the absolute values of the coefficients of $p$, and appending $[\ ]$ at its end retrieves the numerical values which are then able to be fed to the \verb+max+
function.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{HEIGHT:=(p,x)->max( map(abs, \{coeffs(p,x)\})[ ]);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[HEIGHT:=(p,x)\to max( map(abs, \{coeffs(p,x)\})[\ ])\] }
\end{maplelatex}
\end{maplegroup}
\end{solu}

Another way of defining function in Maple is by means of {\em procedure} declarations. We will actually prefer this method rather than the arrow method, since
this method is akin to the ones used by most computer languages. To declare a procedure, we use the syntax \verb+name:=proc(inputs) instructions  end;+.
For example, to declare the assignment rule $f(x)=3x+1$ we write \verb|f:=proc(x) 3*x+1 end;|. A procedure usually returns its last evaluation, a
behaviour which can be bypassed with the  \verb+RETURN+ statement. The values of the input parameters cannot be changed by the procedure, and hence, if they
need to be modified somehow one must first make copies of them.

\begin{exa} \label{exa:swap}
Write a procedure \verb+SWAP(x,y)+ which takes two numbers $x$ and $y$ and exchanges them.
\end{exa}
\begin{solu}
This is a classic algorithm in introductory programming. A standard trick is to create a temporary variable, store the contents of $x$ there, store the contents
of $y$ in $x$, and finally, store the contents of the temporary variable in $y$. Since we cannot change the values of $x$ and $y$ because they are input parameters,
we make copies of these variables into $x1$ and $y1$.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{inert}{1d}{\maplemultiline{> SWAP:=proc(x,y)  \\   x1:=x; y1:=y;\\  temp:=x1; x1:=y1; y1:=temp;\\    RETURN(x1,y1);\\ end;}}{}
\end{mapleinput}
\end{maplegroup}

\end{solu}


\begin{exa}\label{exa:ITHDIGIT}
Write a Maple procedure $ITHDIGIT(x,i)$ that takes a positive integer $x$ and gives its $i$-th digit when read  from right to left.
\end{exa}
\begin{solu} The idea is the following. Consider a positive integer, for example $23456789785765$, and let us find its four digit from the left (it is obviously $5$, but let's
forget that for a minute. The trick is to move the decimal point four units left, obtaining $2345678978.5765$. Now we delete the integral part, obtaining
$.5765$. We now move the decimal point one unit to the right, obtaining $5.765$. The digit we want is the integral part of this last number, namely, $5$.
Here is the code for that set of instructions.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{inert}{1d}{ITHDIGIT:= proc(x, i) b:=x*10^(-i); n:=b-floor(b); z:=10*n; RETURN(floor(z));   end;}{}
\end{mapleinput}
\end{maplegroup}

We can write the code more succinctly as

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{inert}{1d}{ITHDIGIT:= proc(x, i) RETURN(floor(10*(x*10^(-i)-floor(x*10^(-i)))));   end;}{}
\end{mapleinput}
\end{maplegroup}
but this makes it somewhat harder to read.

\end{solu}
For our next example we need to be able to find the number of digits used when writing a given positive integer $x$. This can be found using the
 Maple command \verb+length(x)+.
\begin{exa}
Write a Maple procedure that will output the set of digits that appear in a positive integer.
\end{exa}
\begin{solu}
The idea is to use example \ref{exa:ITHDIGIT} and find every digit. Since a set does not include repetitions, we shew our output in a set.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{inert}{1d}{\maplemultiline{SETOFDIGITS:=proc(x)\\  \qquad RETURN(\{seq(ITHDIGIT(x,i),i=1..length(x))\}); \\ end;}}{}
\end{mapleinput}
\end{maplegroup}

In order to use this procedure, we must type the code of \verb+ITHDIGIT+ prior to it.

\end{solu}


\section*{\psframebox{Homework}}
\begin{multicols}{2}\columnseprule 1pt \columnsep 25pt\multicoltolerance=900

\begin{exer}
Let $A=\{1,2,3,4\}$, $B=\{3,4,5,6\}$, and $f(x)  =\dfrac{x}{x^2+1}$.
Write Maple code that will find $f(A \setminus B) \cup f(B\setminus
A) $ and $f((A \setminus B) \cup (B\setminus A))$.


\begin{answerexer} One may use the following code. We omit the Maple output.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{A:=\{1,2,3,4\};}{}
\mapleinline{active}{1d}{ B:=\{3,4,5,6\};}{}
\mapleinline{active}{1d}{map(x->f(x), A minus B) union  map(x->f(x), B minus A);}{}
\mapleinline{active}{1d}{map(x->f(x), (A minus B) union (B minus A)) ;}{}
\end{mapleinput}
\end{maplegroup}


\end{answerexer}

\end{exer}
\begin{exer}
Given a list of data $[x_1, x_2, \ldots , x_n]$, their {\em
variance} is given by $\dfrac{\sum _{k=1} ^n (x-\mu)^2}{n}$, where
$\mu$ is the average of the $x_k$. Write Maple code to compute the
variance of a given list.

\begin{answerexer} One may use the following code. We omit the Maple output.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{MU:=proc(X) sum(X[i], i=1..nops(X))/nops(X) end;}{}
\mapleinline{active}{1d}{VARIANCE:= proc(X) sum((X[i]-mu(X))^2, i=1..nops(X))/nops(X) end;}{}
\end{mapleinput}
\end{maplegroup}

\end{answerexer}
\end{exer}

\begin{exer}
{\em Without introducing a temporary variable}, write a procedure \verb+SWAP2(x,y)+ that swaps the values of two
variables. For example, if $x=1$ and $y=2$, then \verb+SWAP2(x,y)+.
prints $x=2$ and $y=1$.
\begin{answerexer}
Here is one way.


\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>SWAP2:=proc(x,y) \\ x1:=x+y; y1:=x; x1:=x1-y1; \\ RETURN(x1,y1); \\ end;}}{}
\end{mapleinput}
\end{maplegroup}

\end{answerexer}
\end{exer}

\begin{exer}
Using example \ref{exa:ITHDIGIT} and the Maple functions \verb+sum()+ and \verb+length()+, write a procedure
\verb+SUMDIGITS(x)+ that computes the sum of the digits of a given positive integer $x$.

\begin{answerexer}
Here is one way.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{SUMDIGITS:= proc(x) RETURN(sum(ITHDIGIT(x,i),i=1..length(x))); end;}{}
\end{mapleinput}
\end{maplegroup}

\end{answerexer}
\end{exer}

\begin{exer}
Using example \ref{exa:ITHDIGIT}, write a procedure \verb+PEELER(x)+ that will ``peel out'' the first and last digit of a positive integer with at least
three digits. Leading zeroes are ignored. For example, \verb+PEELER(1234)+ will return $23$ and  \verb+PEELER(1023014)+ will return $23014$ (ignoring the leading $0$ obtained).
\begin{answerexer}
Here is one way. Observe that $a- (a \mod 10)$ deletes the last digit of $a$ replacing it with a zero, and so, \mbox{$(a-a\mod b)/10$} deletes the last digit
of $a$. Furthermore, the integer \verb+ITHDIGIT(b, length(n))*10^(length(b)-1)+ has as many digits as $b$ and has the same leftmost digit of $b$. Thus
\verb+b-ITHDIGIT(b, length(b))*10^(length(b)-1)+ deletes the first digit of $b$. We need to apply these two operations in sequence.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{PEELER:=proc(x)\\ a:=x;\ b:= (a- (a\ mod\ 10))/10;\\ RETURN(b-ITHDIGIT(b, length(b))*10^(length(b)-1));\\  end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}
\end{exer}



\end{multicols}



\section{Conditional Statements and the ``for'' Loop}
A {\em boolean expression} is one that evaluates either {\tt true}
or {\tt false}. We can form boolean expressions with the relation
operators

\begin{tabular}{ll} $=$ & equal to \\
$<$ & less than\\
$<=$ & less than or equal to\\
$>$ & greater than\\
$>=$ & greater than or equal to\\
$<>$ & not equal to\\
\end{tabular}
\begin{rem}
Do not confuse the assignment statement $:=$ with the equality checking operator $=$.
\end{rem}
The standard logic rules hold for these operations.


\bigskip

The conditional statement in Maple takes various forms. The shortest is
\begin{center}
\verb+if (condition) then (commands) fi;+. \end{center}
Other forms are \begin{center} \verb+if (condition) then (commands) else (commands) fi;+  \end{center}and
\begin{center} \verb+if (condition) then elif (commands) elif (commands) ... else (commands) fi;+.  \end{center}


\begin{exa}\label{exa:maxi}
Suppose you didn't know anything about Maple's maximum $\max$
function. Write Maple code that will find the maximum of two
numbers.
\end{exa}
\begin{solu} Here is one possible answer.


\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>MAXI:=proc(x,y) \\ \qquad if x>=y \\ \qquad then \quad  RETURN(x); \\ \qquad else \quad   RETURN(y);\\ \qquad fi; \\ end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{solu}



\begin{exa}
Write Maple code to evaluate the following piecewise assignment rule:
$$f(x) = \left\{\begin{array}{ll}  -2 & \mathrm{if}\quad  x \leq -3 \\
x^2 & \mathrm{if}\quad  -3< x \leq 2 \\
2 & \mathrm{if}\quad  2<x< 4 \\
1 & \mathrm{if}\quad  x > 4 \\
     \end{array}\right.  $$
\end{exa}
\begin{solu} Here is one possible answer.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>f:=proc(x)\\ \qquad   if\quad  x<=-3\\  \qquad\qquad    then \quad  -2 \\ \qquad \qquad\quad       elif\quad   x<=2 \\ \qquad\qquad \qquad        then\quad   x^2 \\ \qquad\qquad \qquad       elif\quad   x<4 \\   \qquad\qquad \qquad \qquad       then\quad   2 \\  \qquad\qquad \qquad \qquad \qquad    else\quad   1 \\   \qquad   fi; \\  end;}}{}
\end{mapleinput}
\end{maplegroup}


Maple has a direct way of declaring a piecewise function, by means
of the command \verb+piecewise()+.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{  f:=x->piecewise(x<=-3,-2,x<=2,x^2,x<4,2,1); }{}
\end{mapleinput}
\end{maplegroup}


\end{solu}



We now investigate our first looping statement. The {\em for} loop has the following syntax, where the \verb+by (step)+
is optional.
\begin{center}\verb+for index from (low) by (step) to (high) do (instructions) od;+.\end{center}

\bigskip

``for'' loops are particularly useful when all data in a certain range must be examined, as in checking the maximum
of list of numbers, or adding numbers in a set.


\begin{exa}
Suppose you didn't know anything about Maple's {\tt sum} command.
Write a Maple procedure to find the sum
$$ 1^2-3^2+5^2-7^2+\cdots -99^2+101^2. $$
\end{exa}
\begin{solu}
We use a temporary variable to store the partial results. We must
initialise it to $0$, otherwise Maple will deposit garbage in it.


\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{> SUMMM:=   proc() \\ \qquad total:=0; \\ \qquad for \quad k\quad  from \quad 1\quad  by\quad  2\quad  to\quad  101 \\ \qquad do\quad  total:=total+(-1)^((k-1)/2)*k^2\quad  od; \\ end;} }{}
\end{mapleinput}
\end{maplegroup}




This can be, of course, accomplished more succinctly with


\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{ sum((-1)^(k+1)*(2*k-1)^2,k=1..51);}{}
\end{mapleinput}
\end{maplegroup}



\end{solu}

\begin{exa}
Give Maple code that will compute the sum of all the integers in $\{1,2,3 \ldots , 1000\}$ which are neither divisible by $3$ nor $5$.
\end{exa}
\begin{solu}
We use the technique of the preceding problem.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{> SECTIONSUM:=   proc() \\ \qquad total:=0; \\ \qquad for\quad  k\quad  from\quad  1\quad   to\quad  1000 \\ \qquad do\quad  if\quad  k\ mod\ 3\ <>0\quad  and\quad  k\ mod\ 5\ <>0\\ \qquad  then\quad  total:=total+k;\\ \qquad  fi; od;\\ \qquad RETURN(total); \\ end;} }{}
\end{mapleinput}
\end{maplegroup}



\end{solu}




\begin{exa}[Linear Search] \label{exa:lin-search}
Write a Maple procedure  \verb+MEMBER(D,w)+ that tests whether a given word \verb+w+ is
a member of a dictionary \verb+D+. Test the program with \verb+L:=[abacus, number, algorithm]+
and the words \verb+algorithm+ and \verb+ossifrage+.
\end{exa}
\begin{solu}
Here is a possible answer.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{> MEMBER:=proc(D,w)\\ \qquad for\ k\ from\ 1\  to\ nops(D)\\ \qquad do\ if\  w=D[k]\ then\ RETURN(true)\\ \qquad  fi;\quad od;\quad  false;\\  end:} }{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{L:=[abacus, number, algorithm]; MEMBER(L,algorithm); MEMBER(L,ossifrage); }{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\begin{array}{c} L := [abacus, number, algorithm]\\ true \\ false \end{array}\] }
\end{maplelatex}
\end{maplegroup}


Of course, this program must be refined to guard against idiotic inputs. Also, it is particularly inefficient, since it searches word for word, and even if
the word has been found, it continues searching. We will see how to improve this later on with the \verb+while+ loop.
\end{solu}

\begin{exa}
A Mersenne prime is a prime of the form $2^p-1$, where $p$ is a
prime. Thus $3=2^2-1, 7=2^3-1, 31=2^5-1$ are all Mersenne primes, but
$2^{11}-1=23\cdot 89$ is not a Mersenne prime. Write a Maple
procedure that generates all Mersenne primes up to $2^{500}-1$. You may avail of Maple's {\tt isprime()} function.
\end{exa}
\begin{solu}
Here is a possible answer.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{> MARINMERSENNE:=proc()\\ \qquad for\ k\ from\ 1\  to\ 500\\ \qquad \qquad  do\ if\ isprime(2^k-1)\ then\ print(2^k-1,"is\ a\ Mersenne\ prime.")\\ \qquad  fi;\ od;\ end:}}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{MARINMERSENNE();}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\begin{array}{c}
                      3, "is\ a\ Mersenne\ prime."\\
                      7, "is\ a\ Mersenne\ prime."\\
                      31, "is\ a\ Mersenne\ prime."\\
                     127, "is\ a\ Mersenne\ prime."\\
                     8191, "is\ a\ Mersenne\ prime."\\
                    131071, "is\ a\ Mersenne\ prime."\\
                    524287, "is\ a\ Mersenne\ prime."\\
                  2147483647, "is\ a\ Mersenne\ prime."\\
             2305843009213693951, "is\ a\ Mersenne\ prime."\\
         618970019642690137449562111, "is\ a\ Mersenne\ prime."\\
      162259276829213363391578010288127, "is\ a\ Mersenne\ prime."\\
   170141183460469231731687303715884105727, "is\ a\ Mersenne\ prime."\\
 \end{array}\] }
\end{maplelatex}
\end{maplegroup}

\end{solu}


\begin{exa} \label{exa:maxi-list}
Write a procedure \verb+MAXILIST(X)+ that determines the maximum entry in a given number list $L$. The procedure must work from scratch, that is,
using Maple's \verb+max()+ function is not allowed.
\end{exa}
\begin{solu}
Here is a possible answer. Observe since at the beginning we had no way of knowing what \verb+maxime+ was, we declare it to be the first element
of the list. That is, we used the first member of the array as a {\em sentinel value}.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>MAXILIST:=proc(X)\\ \qquad maxime:=X[1];\\ \qquad for\ k\ from\ 1\ to\ nops(X)\\ \qquad  do\ if\ X[k]> maxime\ then\ maxime:=X[k];\\ \qquad fi;\ od;\\ \qquad  RETURN(maxime);\\   end;}}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{ X:=[-10,-90,98,2]: MAXILIST(X);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[98\] }
\end{maplelatex}
\end{maplegroup}
\end{solu}




\begin{exa}[The Locker-room Problem] A locker room contains $n$ lockers, numbered $1$ through $n$.
Initially all doors are open. Person number $1$ enters and closes
all the doors. Person number $2$ enters and opens all the doors
whose numbers are multiples of $2$. Person number $3$ enters and if
a door whose number is a multiple of $3$ is open then he closes it;
otherwise he opens it. Person number $4$ enters and changes the
status (from open to closed and viceversa) of all doors whose
numbers are multiples of $4$, and so forth till person number $n$
enters and changes the status of door number $n$.  Write an
algorithm  to determine which lockers  are closed.
\end{exa}\begin{solu} Here is one possible approach. We use an array  of size $n$ to denote the lockers so that we may modify the status of the entries.
 The value {\bf true} will denote an open
locker and the value {\bf false} will denote a closed
locker. We first close all the doors.


\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>LOCKERS:=proc(n)\\ \qquad X:=array([seq(false, k=1..n)]); \\ \qquad for\ j\ from\ 2\ to\ n \\ \qquad \qquad do\ for\ k\ from\ j\ to\ n\ do\\ \qquad \qquad \qquad  if\ k\ mod\ j\ =\ 0\ then\ X[k]:=not(X[k]);\ fi;\ od;\ od; \\ \qquad for\ k\ from\ 1\ to\ n\   do\ if\ not(X[k]) \\ \qquad \qquad \qquad  \qquad then\ print(k,"is\  open.");\ fi;\ od;\\ end;}}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{LOCKERS(100); }{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[\begin{array}{c}
                            1, "is\ open."\\
                            4, "is\ open."\\
                            9, "is\ open."\\
                           16, "is\ open."\\
                           25, "is\ open."\\
                           36, "is\ open."\\
                           49, "is\ open."\\
                           64, "is\ open."\\
                           81, "is\ open."\\
                           100, "is\ open."\\
\end{array}\] }
\end{maplelatex}
\end{maplegroup}


Notice that if  $d$ divides $n$ so does $\dfrac{n}{d}$. Thus we can pair up every the different  divisors of $n$, and have an even number of divisors as long as
we do not have $d=\dfrac{n}{d}$. This means that the integers with an even number of divisors will have all doors open, and those with an odd number of divisors will all
all doors closed. This last event happens when $d=\dfrac{n}{d}\implies n=d^2$, that is, when $n$ is a square.

\end{solu}






\section*{\psframebox{Homework}}
\begin{multicols}{2}\columnseprule 1pt \columnsep 25pt\multicoltolerance=900

\begin{exer}
Suppose you didn't know anything about Maple's absolute value {\tt
abs()} function. Write a  Maple procedure \verb+AbsVal(x)+  that will find the absolute value
of a real number $x$.
\begin{answerexer} Here is one possible answer.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{ABSVAL:= proc(x,y) if x>=0 then RETURN(x) else RETURN(-x) fi; end;}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}
\end{exer}
\begin{exer}
Using Maple's \verb+ithprime()+ function, write a procedure that
writes the first $N$ primes.
\begin{answerexer}
Here is a possible answer.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{PRIMES:= proc(N) for k from 1 to N do print(ithprime(k)) od;  end;}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}
\end{exer}

\begin{exer}
Nest the \verb+MAXI+ procedure of example \ref{exa:maxi} into a new procedure \verb+MAXI3(x,y,z)+ that finds the maximum of three real numbers.
\begin{answerexer} Here is one possible answer.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{ MAXI3:= proc(x,y,z)\\ \qquad \qquad\qquad   MAXI:=proc(a,b)\  if a>=b\  then\   RETURN(a);\  else\   RETURN(b);\ fi\;  end;\\ \qquad if\ MAXI(x,y)>=z\ then\ MAXI(x,y)\\ \qquad  else\ z\ fi;\\ end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}
\end{exer}


\begin{exer}
A {\em twin prime} is a prime $p$ such that $p+2$ is also a prime. Write a Maple procedure to count all the twin primes between $1$ and $1000000$. You may use Maple's
\verb+isprime()+ function. \footnote{It is not known whether the number of twin primes is infinite. Viggo Brunn proved, however, that the infinite series
$\sum _{p\ \mathrm{is\ a \ twin \ prime}} \dfrac{1}{p}$ converges.}
\begin{answerexer} Here is one possible answer.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{TWINPRIMES :=proc() count:=0;\\ \qquad \qquad for\ k\ from\ 1\ to\ 1000000\\ \qquad \qquad  do\ if\  isprime(k)\ and\ isprime(k+2)\ then\ count:=count+1;\\ \qquad fi;\ od;\\ \qquad  RETURN(count);\\  end;}}{}
\end{mapleinput}
\end{maplegroup}

\end{answerexer}

\end{exer}


\begin{exer}
For $n\geq 1$, set $!n= \sum _{k=0} ^{n-1} k!$. Duro Kurepa
conjectured that $\gcd (!n, n!) = 2$  for all $n\geq 2$. This has
been verified for all $n<1000000$. Using Maple's \verb+gcd(a,b)+ function write a procedure to verify this
up to $n=150$. (For larger values, you may get a compilation error
depending on your processor.)
\begin{answerexer}
 Here is a possible Maple\TM procedure.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{KUREPA:= proc(A)  for a from 1 to A do if gcd(sum(k!, k=0..a-1), a!) <> 2 then print('a '=a)  fi; od; end;}{}
\end{mapleinput}
\end{maplegroup}
Take $A\leq 150$.
\end{answerexer}
\end{exer}


\end{multicols}

\section{The ``while'' Loop}
The \verb+while+ loop has syntax \verb+while (condition-true) do (statements) od;+.


\begin{exa}\label{exa:harmonic-sum}
It is known that the harmonic series $\sum _{k \geq 1} \dfrac{1}{k}$ diverges. Find the smallest $N$ for which
$$\sum _{1\leq k \leq N} \dfrac{1}{k} \geq 10. $$
\end{exa}
\begin{solu}
Here is one possible answer. We use a ``while'' loop to detect the very first time that the sum exceeds $10$.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>HARMONIC:=proc(n) \\ \quad sum:=0;\ k:=0; \\ \quad while\ sum <= n \\ \qquad \quad do\ sum:=sum+1/(k+1);\ k:=k+1\ od;\\ \qquad RETURN(k); \\  end;}}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{HARMONIC(10); }{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[12367\] }
\end{maplelatex}
\end{maplegroup}

In Calculus II  you learn  that $1 + \dfrac{1}{2}+\dfrac{1}{3}+\cdots + \dfrac{1}{n}=  \log n + \gamma + \boo{\dfrac{1}{n}}$. Here
$\gamma \approx 0.57721566490153286\ldots $ is the Euler-Mascheroni constant.\footnote{Another open problem, it not known whether $\gamma$ is irrational.}
Hence we need $log n \approx 10-\gamma \implies n \approx e^{10-.577}\approx 12620$, not far from the value Maple found.

\end{solu}




\begin{exa}By availing of the Maple {\tt isprime()}
function, write a procedure that determines the first prime
greater than $1,000,000,001$.
\end{exa}
\begin{solu}
Here is one possible way.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>FIRSTPRIME:= proc()\\ \qquad for\ k\ from\ 1000000001\ by\ 2\\ \qquad \qquad while\ not(isprime(k))\ do\ od;\\ \qquad \qquad RETURN(k)\\ end;}}{}
\end{mapleinput}
\end{maplegroup}
Notice the flexibility of Maple's \verb+for+ loop allowing a \verb+while+ loop as an upper bound.

Maple can accomplish this with just one line.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{nextprime(1000000001); }{}
\end{mapleinput}
\end{maplegroup}

\end{solu}


\begin{exa}The numbers
$$1, 2, 3, \ldots , 2003$$
are written on a blackboard, in increasing order. Then the first, the fourth, the seventh, etc. are erased,
leaving the numbers
$$2, 3, 5, 6, 8, 9, 11, 12, 14, \ldots$$
on the board. This process is repeated, leaving the numbers
$$3, 5, 8, 9, \ldots .$$
The process continues until one number remains on the board and is finally erased. What is the last number
to be erased?
\end{exa}
\begin{solu}
We first write a procedure \verb+SHRINKLIST()+ that is one iteration of the instructions. For example, if one is given the list
$X:=[1,2,3,4,5,6,7,8,9,10]$, then \verb+SHRINKLIST(L)+ returns $[2,3,5,6,8,9]$. We then keep shrinking the array until we find the number we want.
 \verb+SHRINKLIST()+ creates a new list $L$ with the non-deleted elements. Here is the procedure \verb+SHRINKLIST()+.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>SHRINKLIST:=proc(A)\\  L:=[\ ];\ newindex:=1;\\ \qquad for\ k\ from\ 1\ to\ nops(A)\\ \qquad\qquad do\ if\ k\ mod\ 3\ <>\ 1\\ \qquad\qquad then\ C[newindex]:=A[k];\ L:=[op(L),C[newindex]];\\ \qquad\qquad newindex:=newindex+1;\  fi;\ od; \\ eval(L); \\ end;}}{}
\end{mapleinput}
\end{maplegroup}

We now complete the process by shrinking the initial list until it has only one element.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>COMPUTELAST:= proc(X) \\ Y:=X;\ while\ nops(Y)>1\\ \qquad  do\ Y:=SHRINKLIST(Y);\ od;\\ RETURN(Y[1]);\\ end;}}{}
\end{mapleinput}
\end{maplegroup}

Upon invoking \verb+COMPUTELAST([seq(k,k=1..2003)])+, we see that the last integer left is $1598$.

\bigskip

Here is how to solve this problem without programming.
Let $J_n$ be the first number remaining after $n$ erasures, so $J_0
= 1$, $J_1 = 2$, $J_3 = 3$, $J_4 = 5$, etc. We prove by induction
that

$$J_{n+1} =  \dfrac{3}{2}  J_n \ \mathrm{if} \  J_n \  \mathrm{is\ even,}$$
and
    $$J_{n+1} =\dfrac{3}{2} (J_n + 1) - 1 \ \mathrm{if} \ J_n\  \mathrm{is\
    odd}.$$
Assume first that  $J_n = 2N$. Consider the number $3N$. There are
initially $N$ smaller numbers $\equiv 1 \mod 3$. So after the first
erasure, it will lie in $2N$-th place. Hence, it will lie in first
place after $n+1$ erasures. Assume now that $J_n = 2N+1$. Consider
$3N+2$. There are initially $N+1$ smaller numbers $\equiv 1 \mod 3$.
So after the first erasure, it will lie in $2N+1$-st place. Hence,
it will lie in first place after $n+1$ erasures. That completes the
induction. We may now calculate successively the members of the
sequence: $1, 2, 3$, $5, 8, 12$, $18, 27, 41, 62$, $93, 140, 210$,
$315, 473, 710$, $1065, 1598, 2397$. Hence $1598$ is the last
surviving number from $1, 2, \ldots , 2003$.


\end{solu}




\begin{exa}\label{exa:ISPALINDROME}
A {\em palindrome} is a strictly positive integer whose decimal expansion is symmetric and does not end in $0$. For example, $2$, $11$, $3010103$, $19988991$ are all palindromes.
Write a Maple procedure \verb+ISPALINDROME(x)+ that determines whether the positive integer $x$ is palindrome.
\end{exa}
\begin{solu} Trying to solve this problem by purely arithmetic functions one runs into the trouble that Maple does not make any distinction between, say,
 the integer
$11$ and the integer $011$. In order to respect repetitions and order, we first convert the integer into a list. The algorithm below is self explanatory, and we
are using the algorithm  \verb+ITHDIGIT+ from example \ref{exa:ITHDIGIT}.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>MAKEMEINTOLIST:= proc(x)\\ L:=[\ ];\\ for\ k\ from\ 1\ to\ length(x)\\ \qquad   do\ L:=[op(L), ITHDIGIT(x, length(x)-k+1)];\  od;\\ eval(L);\\  end; }}{}
\end{mapleinput}
\end{maplegroup}

Now, we can simply determine whether $x$ is a palindrome by comparing \verb+L[k]+ with \verb|L[nops(L)-k+1]|.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>ISPALINDROME:= proc(x)\\ L:=MAKEMEINTOLIST(x);\\ for\ k\ from\ 1\ to\ length(x)/2\\ \qquad  do\ if\ L[k]<>L[nops(L)-k+1]\ then\ RETURN(false);\   fi;\   od; \\ true;\\  end;}}{}
\end{mapleinput}
\end{maplegroup}


\end{solu}

\begin{exa}\label{exa:reverse-array}
An array $X:=(x_1,x_2,\ldots , x_n)$ is given. Write a procedure that reverses the elements of $X$, that is, that
returns $(x_n, x_{n-1}, \ldots , x_1)$.
\end{exa}
\begin{solu}
The trick is to swap $(x_1,x_n)$, $(x_2,x_{n-1})$, etc.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>REVERSELIST:=proc(X)\\ \qquad  Y:=X;\ left:=1;\ right:=nops(X); \\ \qquad \qquad while(left<right)\ do\\ \qquad temp:=Y[left];\  Y[left]:=Y[right];\  Y[right]:=temp;\ \\ \qquad \qquad left:=left+1;\ right:=right-1;\ od;\\ \qquad\qquad\qquad  eval(Y); \\ end;}}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{REVERSELIST([1,2,5]);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[[5,2,1]\] }
\end{maplelatex}
\end{maplegroup}
\end{solu}



\section*{\psframebox{Homework}}
\begin{multicols}{2}\columnseprule 1pt \columnsep 25pt\multicoltolerance=900

\begin{exer}[Digit Reversing]\label{exer:reverse}
Write a Maple procedure \verb+REVERSEDIGITS+ that prints the digits of a positive integer
in reverse order. Thus  \verb+REVERSEDIGITS(123)+  will print $321$. The program interprets, say, $01230$ as $1230$
 and so you should have \verb+REVERSEDIGITS(1230)+ return $321$.
\begin{answerexer}
Here is a possible answer.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{> REVERSEDIGITS:=proc(n)\\ \qquad b:=n;\ new:=0; \\ \qquad while b<>0\ do\ r:= b mod 10;\ b:= floor(b/10);\\ \qquad new:= new*10 +r ;\ od;\\ \qquad RETURN(new);\\ end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}
\end{exer}
\begin{exer}By strictly arithmetic means, that is, without using lists and without using example \ref{exa:ITHDIGIT}, write a Maple procedure
 \verb+FIRSTISLAST(x)+ that
checks whether the first and the last digit of the  integer $x>0$ (with at least two digits), are equal. The program interprets, say, $01230$ as $1230$
 and so you should have \verb+FIRSTISLAST(01230)+ return false. \begin{answerexer}
Here is a possible answer. The last digit of $x$ is $x \mod 10$. Its first digit is $\floor{x/10^{\mathrm{length(x)}-1}}$.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>FIRSTISLAST:=proc(x)\\ if\ (x\ mod\ 10)\ = floor(x/10^(length(x)-1))\\ \qquad  then\ RETURN(true)\\ \qquad else\ RETURN(false)\ fi;\  end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}
\end{exer}

\begin{exer}
Using Maple's {\tt rand(low..high)} function for producing a random
number from {\tt low} to {\tt high}, simulate the toss of a die $n$
times.
\begin{answerexer}
Here is a possible answer.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{DIETOSS:=proc(n)  die:=rand(1..6); k:=1;while(k<=n)  do k:=k+1 ; print(die()); od; end;}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}

\end{exer}

\begin{exer}
Using example \ref{exa:ISPALINDROME}, find the sum of all palindromes between $M$ and $N$, with $M<N$.
\begin{answerexer}
Here is a possible answer.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>SUMPALINDROMES:= proc(M, N)\\ total:=0;\\ for\ k\ from\ M\ to\ N\ do\\ \qquad  if\ ISPALINDROME(k)\ then\ total:=total+k;\ fi;\  od;\\ RETURN(total);\\ end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}


\end{exer}

\begin{exer}[Goldbach's Conjecture] It is an unsolved problem to shew that every even integer $n\geq 6$
can be written as the sum of two odd primes. Using Maple's \verb+isprime()+ function, write a Maple program to verify Goldabach's conjecture for any even integer
$\leq 1000000$.
\begin{answerexer}
Here is a possible answer.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>GOLDBACH:=proc(n)\\  for\ k\ from\ 3\ to\ (n-3)\\ do\ if\ isprime(k)\ and\ isprime(n-k)\ then\ print(n, "=", k,"+", n-k)\ fi;\ od;\\ end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}

\end{exer}



\begin{exer}[Postage Problem] Suppose that you have two types of postage stamps: one costing $a$ cents and the other $b$ cents. We say that a postage of $h$ cents is
{\em realisable} if there are positive integral solutions $x, y$ to the equation $ax+by=h$. Write a Maple procedure \verb+POSTAGE(a,b,h)+
in which you input three positive integers
$a, b, h$ and tell whether the postage of  $h$ cents is realisable with stamps costing $a$ and $b$ cents.
\begin{answerexer}
Here is a possible answer.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>POSTAGE:=proc(a, b, h)\\  realisable:=false;\ x:=-1;\\ \qquad while(x<=h/a\ and\ not(realisable))\ do\ x:=x+1;\ y:=-1;\\ \qquad while(y<=h/b\ and\ not(realisable))\ do\ y:=y+1; \\ if\ h=a*x+b*y\ then\ realisable:=true;\ fi;\\ \quad od;\ od;\\ print(h,"is ", a, "*", x, "+", b, "*", y);\\  end;}}{}
\end{mapleinput}
\end{maplegroup}

\end{answerexer}
\end{exer}
\begin{exer}[Circle Problem]
Given a positive integer $n$ write a Maple program that counts the number of solutions  to $x^2+y^2\leq n$ where $x, y$ are both positive integers.
\begin{answerexer}
Here is a possible answer.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>CIRCLEPROBLEM:=proc(n)\\  a:=0;\  s:=0;\\ while(a*a<=n)\ do\  b:=0;\ t:=0;\\ while (a*a+b*b<=n)\ do\ b:=b+1;\ t:=t+1;\ od;\\ a:=a+1;\ s:=s+t;\ od;\\RETURN(s);\\ end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}
\end{exer}
\begin{exer}
Write a Maple procedure that gives the Roman numeral representation
of any integer between $1$ and $3999$.
\begin{answerexer}Our algorithm works as follows: the maximum number of consecutive repetitions in a roman numeral is three, and so every number in the given range can be formed
with one of the strings in $[M, CM,D,CD,C,XC,L,XL,X,IX,V,IV,I]$.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{> ROMAN:= proc(n)\\  romannumeral:=[\ ];\ hindunumber:=n;\\ a:=[1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];\\  r:=[M, CM,D,CD,C,XC,L,XL,X,IX,V,IV,I];\\ for\ k\ from\ 1\ to\ nops(a)\\ do\ while\ hindunumber >= a[k]\\  do\ hindunumber:=hindunumber-a[k];\\  romannumeral:=[op(romannumeral),r[k]];\ od;\ od; \\ RETURN(romannumeral[\ ]);\\ end;}}{}
\end{mapleinput}
\end{maplegroup}
We can do this more efficiently with Maple's \verb+convert+ command.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{convert(1966,roman);}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}
\end{exer}

\begin{exer}\label{exa:switch-array2}
A list $$X:=(x_1,x_2,\ldots , x_m, x_{m+1}, x_{m+2}, \ldots , x_{m+n})$$ is given. Write a procedure \verb+SWITCHLIST(X,m,n)+ that, for given  subscripts $m$ and $n$,
it will return  $$(x_{m+1}, x_{m+2}, \ldots , x_{m+n}, x_1, x_2, \ldots , x_m).$$
\begin{answerexer}We use the procedure \verb+REVERSELIST+ from example  \ref{exa:reverse-array}. We first revert the portions $(x_1,x_2,\ldots , x_m)$ to
$(x_m,x_{m+1},\ldots , x_1)$ and $(x_{m+1}, x_{m+2}, \ldots , x_{m+n})$ to $(x_{m+n},x_{m+n-1},\ldots , x_{m+1})$. We concatenate them to
$$(x_m,x_{m+1},\ldots , x_1 , x_{m+n},x_{m+n-1},\ldots , x_{m+1}),$$ and we revert this last array to
$$(x_{m+1}, x_{m+2}, \ldots , x_{m+n}, x_1, x_2, \ldots , x_m)$$which is what we wanted.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>SWITCHLIST:=proc(X,m,n)\\ Y:=X;\\ L1:=REVERSELIST(X[1..m]);\\ L2:=REVERSELIST(X[m+1..m+n]);\\ L:=REVERSELIST([op(L1),op(L2)]); \\ RETURN(L); \\ end;}}{}
\end{mapleinput}
\begin{mapleinput}
\mapleinline{active}{1d}{SWITCHLIST([1,2,3,4,5,a,b,c,d,e,f,g,h,i,j],5,10);}{}
\end{mapleinput}
\mapleresult
\begin{maplelatex}
\mapleinline{inert}{2d}{}{\[[a, b, c, d, e, f, g, h, i, j, 1, 2, 3, 4, 5]\] }
\end{maplelatex}
\end{maplegroup}
\end{answerexer}
\end{exer}
\begin{exer}
An array $X=(x_1,x_2, \ldots , x_n)$ is given, sorted such that $x_1\leq x_2\leq \cdots \leq x_n$. Count the number of different
$x_k$. \begin{answerexer}
Here is a possible answer.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{> DIFFERENT:= proc(X)\\ i:=1;\ dif:=1;\\  while\ i<> nops(X)\\ do\ i:=i+1;\ if\ X[i]<>X[i-1]\ then\ dif:=dif+1;\ fi;\  od;\\   end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}
\end{exer}
\begin{exer}
Two given lists $X:=(x_1,\ldots x_k)$ and $Y:=(y_1, \ldots y_l)$ are sorted so that
$x_1<\cdots < x_k$ and $y_1<\cdots y_l$. Find how many elements in common they have, that is, find the cardinality of their intersection.
\begin{answerexer}
Here is a possible solution.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{LISTCOMMONERS:= proc(X,Y)\\ k1:=0;\ l1:=0;\ n:=0;\\ while\ (k1 <> nops(X))\ and\ (l1<> l)\\ do\ if\ X[k1+1]<Y[l1+1]\\ then\ k1:=k1+1;\\ elif X[k1+1]>Y[l1+1]\\ then l1:=l1+1;\\ else\ k1:=k1+1;\ l1:=l1+1;\ n:=n+1;\ fi;\  od;\\ RETURN(n);\\ end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}
\end{exer}
\begin{exer}
What is the smallest positive integral power of $7$ whose first
three digits (from left to right) are $222$? In general, write a
Maple procedure that given strictly positive integers $a$ and $x$
will compute the smallest positive integral power of $x$ that will
begin with $a$.
\begin{answerexer}
Here is a possible solution.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{a:=proc(a,x)  k:=1; while
floor(x^k/10^(length(x^k)-length(a))) <> a do k:=k+1; od; RETURN(k);
end;}{}
\end{mapleinput}
\end{maplegroup}
\end{answerexer}
For $a=222$, the least power is $k=327$. The program seems to take a
long time to compute various values.
\end{exer}



\end{multicols}

\section{Iteration and Recursion}
A {\em recursive procedure} is one where future steps are computed and rely on entirely on previously computed steps. An {\em iterative procedure} is one
which is obtained by repetition of a code fragment. These definitions are imperfect, but we hope they will become clearer with some examples.

\begin{exa}[Fibonacci Numbers]
The {\em Fibonacci Numbers} are defined recursively by
$$f_0 = 0, \quad f_1=1, \quad f_{n+1}=f_n+f_{n-1}, \qquad n\geq 1,  $$
so the sequence goes
$$0,1,1,2,3,5,8,13,21,34,55,89\ldots  $$
Write a Maple program that computes the $n$-th Fibonacci number.
\end{exa}
\begin{solu}
Here is an iterative solution.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>FIBONEI:=proc(n) \\ if n<=1 then\    f:=n; \\ else\ fold:=0;\   fnew:=1; \\   for\ k\ from\ 2\ to\  n \\          do\ f:= fold+fnew;\ fold:=fnew;\ fnew:=f;\  \\ od;\        fi;  \\ RETURN(f); \\ end; }}{}
\end{mapleinput}
\end{maplegroup}

Here is a recursive solution.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{> FIBONEII:=proc(n) \\ if\ n<=1\  then\    n;  \\     else\ FIBONEII(n-2)+FIBONEII(n-1); \\       fi; \\ end;}}{}
\end{mapleinput}
\end{maplegroup}
It is worth to compare the running time between the two programs. For this, use Maple's \verb+time()+ command.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{time(FIBONEI(20));}{}
\mapleinline{active}{1d}{time(FIBONEII(20));}{}
\end{mapleinput}
\end{maplegroup}


Observe that the recursive program is much slower. In fact, if I try to compute \verb+FIBONEIi(200)+, my computer takes several minutes. This is because
each time \verb+FIBONEIi()+ is called, Maple has to recalculate the preceding values, without remembering them. If you use  the option {\tt remember}, then
the programm runs much faster.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{> FIBONEIII:=proc(n)\\ option\ remember; \\ if\ n<=1\  then\    n;  \\     else\ FIBONEIII(n-2)+FIBONEIII(n-1); \\       fi; \\ end;}}{}
\mapleinline{active}{1d}{time(FIBONEIII(2000));}{}
\mapleinline{active}{1d}{time(FIBONEI(2000));}{}
\end{mapleinput}
\end{maplegroup}
\end{solu}

\begin{exa}[Horner's Method]
Write an iterative algorithm $Horner(p,x0)$ to evaluate a polynomial $$p:=x\mapsto a_0 +a_1x+a_2x^2+\cdots + a_nx^n$$ at $x=x_0$.
\end{exa}
\begin{solu}
Observe that we may successively compute
$$a_n, \  a_nx_0+a_{n-1}, \ x_0(x_0a_n+a_{n-1})+a_{n-2}, \ x_0( x_0(x_0a_n+a_{n-1})+a_{n-2})+a_{n-3},\ \ldots \,    $$
each time multiplying the preceding result by $x_0$ and adding a constant. We enter the coefficients of the polynomial in a list $p:=[a_0, a_1, \ldots, a_n]$, and carry out the
instructions just described. Observe that $n+1=nops(p)$ and that $a_k=p[k+1]$. Here is the code.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>HORNER:=proc(p,x0)\\ total:=0; \\ for\ k\ from\ 1\ to\ nops(p)\\  do\ total:= total*x0+ p[nops(p)-k+1];\ od;\\    end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{solu}
\begin{exa}[Collatz Conjecture] Consider the function  $f:\BBN\to \BBN$,
$$ f(n)=\left\{\begin{array}{ll} \dfrac{n}{2} & \mathrm{if}\ n\ \mathrm{is\ even}, \\
 3n+1 & \mathrm{if}\ n\ \mathrm{is\ odd}. \end{array}\right. $$
If one considers the sequence
$$ n,\qquad  f(n),\quad  f(f(n)),\quad  f(f(f(n))), \ldots ,$$ it is not known whether this sequence will ultimately end with a $1$.
Write a Maple program that computes this sequence until it halts with a $1$ (if at all).
\end{exa}
\begin{solu}
Here is a possible solution.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>COLLATZ:=proc(n)\ x:=n;\\  for\ k\ from\ 1\ while\ x>1\ do\ print(x);\\  if\ x\ mod\ 2\ =1\ then\ x\ :=3*x+1;\ else\ x:=x/2;\ fi;\\  od;\\  end;}}{}
\end{mapleinput}
\end{maplegroup}


\end{solu}







\section*{\psframebox{Homework}}
\begin{multicols}{2}\columnseprule 1pt \columnsep 25pt\multicoltolerance=900
\begin{exer}\label{exer:factorial} Write two procedures, one iterative and the other recursive, for
computing $n!$ for an integer $n\geq 0$. Your
procedure cannot involve the operator $!$.
\begin{answerexer}
Here is an iterative one.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{ fact1:=proc(n)  f:=1;\\  if n<=1\ then\ f;  \\ \qquad else\ for\ k\ from\ 1\ to\  n\ do\ f:=k*f;\ od;\  fi; \\ RETURN(f);\\ end;}}{}
\end{mapleinput}
\end{maplegroup}



Here is a recursive  one.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{fact2:=proc(n)\\ option\ remember;\\ if\ n<=1\ then\ 1\\ else\ n*fact2(n-1)\ fi; \\ end;}}{}
\end{mapleinput}
\end{maplegroup}


By typing
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{  time(fact1(200));\ time(fact2(200));}{}
\end{mapleinput}
\end{maplegroup}
we see that the iterative version is somewhat faster.






\end{answerexer}
\end{exer}


\end{multicols}


\section{Some Classic Algorithms}



We now examine some classic algorithms. Among them we will find Eratosthenes' Sieve, Euclid's Algorithm and several sorting algorithms. Maple has many of these algorithms
as built-in functions, but the question arises: how  is Maple able to perform these feats? How are its functions written? Our purpose
 is to learn to reinvent the wheel, but without trying to go overboard and get distracted by too many details. We omit an important topic, to prove whether our algorithms are {\em correct}. The interested reader may consult
 \cite{CLRS} or \cite{Knu} for this topic.





\begin{exa}[Eratosthenes' Sieve]\label{exa:eratos}
Let $n>0$ be a composite integer. Then we may factor $n$ as $n=ab$
with positive integers $1<a\leq b < n$. Let $p$ be the smallest
prime factor dividing $n$. Then $p^2\leq ab =n$, and so $n$ has a
prime factor  $p \leq \sqrt{n}$. This means that if a positive
integer has no divisor less than or equal its square root, then it
is a prime.

\bigskip

For example, to test whether $103$ is prime, we divide $103$ by every positive integer between $2$ and $\floor{\sqrt{103}} = 10$. Since $103$ is not divisible by
any integer in the interval  $[2;10]$ we conclude that $103$ is prime.

\bigskip

Write a Maple program that tests for primality of a
positive integer.
\end{exa}
\begin{solu}
Here is a possible approach.

\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{TISPRIME:= proc(x) \\ \qquad  k:=2;\ flag:=true; \\ \qquad   if\ x=1\ then\ print("1\ is\ a\ unit."); \\ \qquad     flag:=false; \\ \qquad  else\ while\ k<=floor(sqrt(x))\ and\ flag  \\ do\ if\ x\ mod\ k\ =\ 0\\ then\  flag:=false;\    fi;k:=k+1;\ od; \\ fi;  \\ \qquad   if(flag)\    then\ print(x, "is\ prime") \\ \qquad  elif\ x>1\\ \qquad         then\ print(x, "is\ divisible\ by ", k-1); \\ \qquad  fi; \\ end;}}{}
\end{mapleinput}
\end{maplegroup}
Maple, of course, has its own internal function \verb+isprime()+ to
test whether an integer is prime.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{ isprime(60637);}{}
\end{mapleinput}
\end{maplegroup}
\end{solu}


\begin{df}
If $a$ and $b$ are two strictly positive integers, then their {\em
greatest common divisor}, denoted by  $\gcd (a, b)$ is the largest
positive integer dividing both $a$ and $b$.
\end{df}

For example, $\gcd (20, 30)=10$, $\gcd (44, 45)=1$ and if $p\neq q$
are primes then $\gcd (p,q)=1$.

\bigskip

Recall that by the Division Algorithm, for all positive integers $a$
and $b>0$, then we can find unique integers $q, r$ called the {\em
quotient} and the {\em remainder}, respectively, such that
$$a=qb+r, \qquad 0 \leq r < b.  $$
For example, if $a=1004, b = 75$ then
$$ 1004=13\cdot 75+29, $$whence $q=13$ and $r=29$.

\bigskip

Let $a, b$ be positive integers. After using the Division
Algorithm repeatedly, we find the sequence of equalities
\begin{equation}
\begin{array}{lcll}  a & = & bq_1 + r_2, & 0 <
r_2 < b, \\ b  & = & r_2q_2 + r_3 & 0 < r_3 < r_2, \\
r_2 & = & r_3q_3 + r_4 & 0 < r_4 < r_3, \\
  \vdots & \vdots & \vdots & \vdots  \\
r_{n - 2} & = & r_{n - 1}q_{n - 1} + r_n & 0 < r_n < r_{n - 1}, \\
r_{n - 1} & =  & r_nq_n. &  \\
\end{array} \label{eq:remainders_euclid}\end{equation}
The sequence of remainders will eventually reach a $r_{n + 1}$
which will be zero, since $b, r_2 , r_3, \ldots$ is a
monotonically decreasing sequence of integers, and cannot contain
more than $b$ positive terms.


The Euclidean Algorithm rests on the fact  that $\gcd(a, b) =
\gcd(b, r_2) = \gcd(r_2, r_3) = \cdots = \gcd(r_{n - 1}, r_n) =
r_n.$


We illustrate it with $a=1004, b = 75$ then
$$ \begin{array}{lll}1004 & = & 13\cdot 75+29,\\
75 & = & 2\cdot 29+17,\\
29 & = & 1\cdot 17+12,\\
17 & = & 1\cdot 12+5,\\
12 & = & 2\cdot 5+2,\\
5 & = & 2\cdot 2+1,\\
2 & = & 2\cdot 1,\\
 \end{array} $$
whence $\gcd (1004,75)=1$.

\begin{exa}[Euclidean Algorithm]\label{exa:euclid}
Write a Maple procedure that takes two strictly positive integers
and returns their greatest common divisor using the Euclidean
Algorithm. You may only use the Maple {\tt mod} function to find
remainders and various multiplications or divisions to find
quotients.
\end{exa}
\begin{solu}
Here is a possible solution.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{EUCLIDALGO:=proc(a, b)\\ x:=a; y:=b;\\ while\ (x\ <>\ 0\ and\ y\ <>\ 0)\ do\ if\ x>=y\ then\ x:= x\ mod\ y\\ else\ y:= y \ mod\ x\ fi;\ od;\\ if\ x=0\ then\ RETURN(y)\ else\ RETURN(x)\ fi; \\ end;}}{}
\end{mapleinput}
\end{maplegroup}

Maple, of course, has its own internal function \verb+gcd(a, b)+ to find
the greatest divisor of two numbers.

\end{solu}


\begin{exa}[Positive Integral Powers] \label{exa:powers2} Write a Maple procedure that will compute $a^n$, where $a$ is a
given real number and $n$ is a given positive integer.
\end{exa}
\begin{solu}:  Here is an approach which essentially reduces computing an $n$-th power to squaring.
 We successively square $x$
getting a sequence
$$ x \rightarrow x^2 \rightarrow x^4 \rightarrow x^8 \rightarrow \cdots \rightarrow x^{2^k},  $$and we stop when $2^k \leq n < 2^{k+1}$. For example, if $n = 11$ we compute
$x \rightarrow x^2 \rightarrow x^4 \rightarrow x^8$. We now write
$11 = 8 + 2 + 1$ and so $x^{11} = x^8x^2x$.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{POWER:=proc(x,n)\\ product:=1;\ c:=x;\ k:=n; \\ while\ k<> 0\ do\ if\ k\ mod\ 2\ =\ 0\\ then\ k:=k/2;\ c:=c*c;\\ else\ k:=k-1;\ product:=product*c;\ fi;\ od;\\ RETURN(product);\\  end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{solu}


We now investigate some sorting algorithms. Sorting algorithms are ubiquitous in applications, for example, alphabetising a list of names, or arranging a sequence of scores
monotonically.

\begin{exa}[Bubblesort]\label{exa:bubble}
We now sort a list $L:=(x_1,x_2,\ldots , x_n)$ of numbers into an increasing sequence.   We proceed naively as follows: we compare  two items at a time
and swap  them if they are misplaced. The pass through the list is repeated until no swaps are needed, thereby sorting the list. We utilise imbricated two \verb+for+ loops,
the first running with index $i$, $1\leq i \leq \mathrm{nops(X)}-1$ and the second running with index $j$, $1\leq j \leq \mathrm{nops(X)}-i$.
For example, to sort the list $[3, 4, 5, 2, 1]$:
\begin{enumerate}
\item The outer loop has four runs: $1\leq i \leq 4$.
\item The list is $[3, 4, 5, 2, 1]$. For $i=1$,  we start with  $3$ and $4$. As they are rightly sorted  we do nothing. We now compare $4$ and $5$. Since
they are  rightly sorted, we do nothing. We continue and compare $5$ and $2$. Since they are wrongly sorted, we swap them, obtaining the new array
$[3, 4, 2, 5, 1]$. We compare now $5$ and $1$, and we swap them since they are wrongly sorted. We obtain the array $[3, 4, 2, 1, 5]$. Notice that this moves the largest
element to the last position.
\item The list is now $ [3, 4, 2, 1, 5]$. For $i=2$,   we again start with   $3$ and $4$. As they are rightly sorted  we do nothing. We now compare $4$ and $5$. Since
they are  rightly sorted, we do nothing. We continue and compare $4$ and $2$. Since they are wrongly sorted, we swap them, obtaining the new array
$[3, 2, 4, 1, 5]$. We compare now $4$ and $1$, and we swap them since they are wrongly sorted. We obtain the array $[3, 2, 1, 4, 5]$.
Notice that this moves the second  largest
element to the ante-penultimate position.
\item The list is now $[3, 2, 1, 4, 5]$. For $i=3$,  we compare  $3$ and $2$. As they are wrongly sorted  we swap them, obtaining
$[2, 3, 1, 4, 5]$.
We now compare $3$ and $1$. Since
they are  wrongly sorted, we swap them, obtaining $[2, 1, 3, 4, 5]$.
\item For $i=4$, the list is now   $[2, 1, 3, 4, 5]$. We compare $2$ and $1$ and swap them, obtaining the sorted list. $ [1, 2, 3, 4, 5]$.
\end{enumerate}
The steps of the outer for loops are thus
$$[3, 4, 5, 2, 1]\to [3, 4, 2, 1, 5] \to [3, 2, 1, 4, 5] \to [2, 1, 3, 4, 5] \to [1, 2, 3, 4, 5].$$
\end{exa}
\begin{solu} Here is a Maple implementation.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{>BUBBLE:= proc(X)\\ Y:=X;\\ for\ i\ from\ 1\ to\ (nops(Y)-1)\\ do\ for\ j\ from\ 1\ to\ (nops(Y)-i)\\ do\ if\ Y[j]>Y[j+1]\ then\ temp:=Y[j];\  Y[j]:=Y[j+1];\  Y[j+1]:=temp;\ fi;\   od;\  od;\\ eval(Y);\\ end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{solu}




\begin{exa}[Quicksort]\label{exa:quickie}
Quicksort is a ``divide-and-conquer'' algorithm for sorting data. One chooses any one number, $x$ say, from the list in question and then shoves the numbers in the list
which are greater than $x$ into one end of the list, and the numbers which are smaller than $x$ into the other end of the list. This partitions the array into two smaller arrays and
then one proceeds to carry out the same instructions into these two smaller arrays, etc.
\end{exa}
\begin{solu}Here is the implementation, directly quoted from \cite{WM}:
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{> partition:=proc(m, n) \ i:=m;\\ j:=n;\ x:=A[j];\ while\ i<j\ do\\ if\ A[i]>x\  then\ A[j]:=A[i];\ j:=j-1;\ A[i]:=A[j];\ else\ i:=i+1\; fi;\ od;\ A[j]:=x; \\ p:=j;\\ end:}}{}
\mapleinline{active}{1d}{\maplemultiline{>QUICKIE:=proc(A,m,n) \\  if\ m< n\ then\ partition(m, n);\ QUICKIE(A,m,p-1); \\  QUICKIE(A,p+1,n); fi;\\  eval(A); \\   end;}}{}
\end{mapleinput}
\end{maplegroup}
\end{solu}







\section*{\psframebox{Homework}}
\begin{multicols}{2}\columnseprule 1pt \columnsep 25pt\multicoltolerance=900
\begin{exer}
Without using Maple's \verb+isprime()+, \verb+ifactor()+ functions, etc., write a procedure that prints the factorisation of a given integer $n>0$
into primes.
\begin{answerexer}
Here is one possible way. We recall that a composite integer $n$ must have a prime factor $\leq \sqrt{n}$.
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{PrimeFactors:=proc(n)\\  k:=n;\ t:=2;\\  while\ not\ k=1\\ \qquad do\ if\ k\ mod\ t=0\ then\ k:= k/t;\ print(t);\\ \qquad \qquad  elif\ t*t > k\ then\ t:=k;\\ \qquad \qquad \qquad else\ t:=t+1;\ fi;\ od;\\ end;}}{}
\end{mapleinput}
\end{maplegroup}




\end{answerexer}

\end{exer}





\end{multicols}



\chapter{Orders of Infinity}
\section{Big Oh and Vinogradov's Notation}


    \fcolorbox{blue}{yellow}{
    \begin{minipage}{.90\linewidth}
    \noindent\textcolor{red}{\textbf{Why bother?}} It is clear that
    the sequences $\seq{n}{n=1}{+\infty}$ and
    $\seq{n^2}{n=1}{+\infty}$ both tend to $+\infty$ as $\ngrows$.
    We would like now to refine this statement and compare one with
    the other. In other words, we will examine their speed towards
    $+\infty$.
\end{minipage}}

Throughout we only consider sequences of real numbers.
\begin{df}
We write $a_n = \boo{b_n}$ if $\exists  C>0$, $\exists N>0$ such
that $\forall n\geq N$  we have $\absval{a_n}\leq C\absval{b_n}$. We
then say that $a_n$ is {\em Big Oh} of $b_n$, or that $a_n$ {\em is
of order at most $b_n$} as $\ngroes$. Observe that this means that
$\absval{\dfrac{a_n}{b_n}}$ is bounded for sufficiently large $n$.
The notation $a_n << b_n$, due to Vinogradov, is often used as a
synonym of $a_n = \boo{b_n}$.
\end{df}
\begin{rem}
A sequence $\seq{a_n}{n=1}{+\infty}$ is bounded if and only if
$a_n<<1$.
\end{rem}
An easy criterion to identify Big Oh relations is the following.

\begin{thm}\label{thm:sufficient-conds-for-big-oh}
If $\lim _{\ngroes} \dfrac{a_n}{b_n} =c\in\BBR$, then $a_n<<b_n$.
\end{thm}
\begin{pf}
Since a convergent sequence is
bounded,  the sequence $\seq{\dfrac{a_n}{b_n}}{n=+1}{+\infty}$
is bounded: so for sufficiently large $n$,
$\absval{\dfrac{a_n}{b_n}}<C$ for some constant $C>0$. This proves
the theorem.
\end{pf}
\begin{rem}
The $=$ in the relation $a_n = \boo{b_n}$ is not a true equal sign.
For example $n^2 = \boo{n^3}$ since $\lim _{\ngroes}
\dfrac{n^2}{n^3} = 0$ and so $n^2<<n^3$ by Theorem
\ref{thm:sufficient-conds-for-big-oh}. On the other hand, $\lim
_{\ngroes} \dfrac{n^3}{n^2} = +\infty$ so that for sufficiently
large $n$, and for all $M>0$, $n^3>Mn^2$, meaning that $n^3\neq
\boo{n^2}$. Thus the Big Oh relation is not symmetric.\footnote{One
should more properly write $a_n\in \boo{b_n}$, as $\boo{b_n}$ is the
set of sequences growing to infinity no faster than $b_n$, but one
keeps the $=$ sign for historical reasons.}
\end{rem}
\begin{thm}[Lexicographic Order of Powers]\label{thm:lexico-powers-big-oh} Let $(\alpha,
\beta)\in\BBR$ and consider the sequences
$\seq{n^\alpha}{n=1}{+\infty}$ and $\seq{n^\beta}{n=1}{+\infty}$.
Then $n^\alpha << n^\beta \iff \alpha \leq \beta$.
\end{thm}
\begin{pf}
If $\alpha \leq \beta$ then $\lim_{\ngroes}
\dfrac{n^\alpha}{n^\beta} $ is either $1$ (when $\alpha = \beta$) or
$0$ (when $\alpha < \beta$), hence $n^{\alpha}<<n^{\beta}$ by
Theorem \ref{thm:sufficient-conds-for-big-oh}.

\bigskip

If $n^{\alpha}<<n^{\beta}$ then for sufficiently large $n$,
$n^{\alpha}\leq Cn^{\beta}$ for some constant $C>0$. If $\alpha
>\beta$ then this would mean that for all large $n$ we would have $n^{\alpha-\beta}\leq
C$, which is absurd, since for a strictly positive exponent
$\alpha-\beta$, $n^{\alpha-\beta}\rightarrow +\infty$ as
$\ngroes$.\end{pf}
\begin{exa}
As $\ngroes$,
$$ n^{1/10}<<n^{1/3}<<n<<n^{10/9}<<n^2, $$for example.
\end{exa}
\begin{thm}\label{thm:constants-matter-not-big-oh} If $c\in\BBR\setminus\{0\}$ then $\boo{ca_n} = \boo{a_n}$, that is, the set of sequences of order at most $ca_n$ is the same set at those of order at most
$a_n$.\end{thm}
\begin{pf}We prove that  $b_n= \boo{ca_n}\iff b_n = \boo{a_n}$. If $b_n= \boo{ca_n}$ the there are constants $C>0$ and
$N>0$ such that $\absval{b_n} \leq C\absval{ca_n}$ whenever $n \geq
N$. Therefore, $\absval{b_n} \leq C'\absval{a_n}$ whenever $n \geq
N$, where $C' = C \absval{c}$, meaning that $b_n=\boo{a_n}$.
Similarly, if $b_n= \boo{a_n}$ the there are constants $C_1>0$ and
$N_1>0$ such that $\absval{b_n} \leq C_1\absval{a_n}$ whenever $n
\geq N_1$. Since $c\neq 0$ this is equivalent to  $\absval{b_n} \leq
\dfrac{C_1}{c}\left(c\absval{a_n}\right)=C''\left(c\absval{a_n}\right)$
whenever $n \geq N_1$, where $C'' = \dfrac{C_1}{c}$, meaning that
$b_n=\boo{ca_n}$. Therefore, $\boo{a_n} = \boo{ca_n}$.
\end{pf}
\begin{exa}
As $\ngroes$,
$$ \boo{n^3}=\boo{\dfrac{n^3}{3}}=\boo{4n^3}. $$
\end{exa}

\begin{thm}[Sum Rule]\label{thm:sum-rule-big-oh}Let $a_n = \boo{x_n}$ and $b_n = \boo{y_n}$. Then $a_n + b_n = O(\max(\absval{x_n}, \absval{y_n}))$.\end{thm}
\begin{pf} There exist strictly positive constants $C_1, N_1, C_2, N_2$ such that
$$n\geq N_1, \implies \absval{a_n} \leq C_1\absval{x_n}\qquad \mathrm{and} \qquad n\geq N_2, \implies \absval{b_n} \leq C_2\absval{y_n}.$$
Let $N' = \max(N_1, N_2)$. Then for $n \geq N$,  by the Triangle
inequality $$\absval{a_n + b_n} \leq \absval{a_n} + \absval{b_n}
\leq C_1\absval{x_n} + C_2\absval{y_n}.$$ Let $C' = \max(C_1,C_2)$.
Then
$$ \absval{a_n + b_n}  \leq  C'(\absval{x_n} + \absval{y_n}) \leq 2C' \max(\absval{x_n}, \absval{y_n}),
$$ whence the theorem follows.
\end{pf}


\begin{cor}\label{cor:leading-term-dominates}Let $a_n= k_0n^m+k_1n^{m-1}+k_2n^{m-2}+\cdots + k_{m-1}n+k_n$ be a polynomial of degree $m$ in $n$ with real number coefficients.
The $a_n=\boo{n^m}$, that is, $a_n$ is of order at most its leading
term.\end{cor}
\begin{pf} By the Sum Rule Theorem \ref{thm:sum-rule-big-oh} the leading term dominates.\end{pf}

\begin{thm}[Transitivity Rule]\label{thm:transitivity-big-oh} If $a_n = O(b_n)$ and $b_n = O(c_n)$, then $a_n = \boo{c_n}$.\end{thm}
\begin{pf} There are strictly positive constants $C_1, C_2, N_1, N_2$  such that
$$n\geq N_1, \implies \absval{a_n} \leq C_1\absval{b_n}\qquad \mathrm{and} \qquad n\geq N_2, \implies \absval{b_n} \leq C_2\absval{c_n}.$$
If $n\geq \max (N_1,N_2)$, then
 $\absval{a_n} \leq C_1\absval{b_n} \leq  C_1C_2\absval{c_n}=C\absval{c_n}$ , with $C=C_1C_2$. This gives  $a_n = \boo{c_n}$.
\end{pf}
\begin{exa}
By Corollary \ref{cor:leading-term-dominates}, $ 5n^4-2n^2+100n-8 =
\boo{5n^4}$. By Theorem \ref{thm:constants-matter-not-big-oh},
$\boo{5n^4}=\boo{n^4}$. Hence $$5n^4-2n^2+100n-8=\boo{n^4}.$$
\end{exa}

\begin{thm}[Multiplication Rule]\label{thm:multiply-big-oh} If $a_n = O(x_n)$ and $b_n = O(y_n)$, then $a_nb_n = \boo{x_ny_n}$.\end{thm}
\begin{pf} There are strictly positive constants $C_1, C_2, N_1, N_2$  such that
$$n\geq N_1, \implies \absval{a_n} \leq C_1\absval{x_n}\qquad \mathrm{and} \qquad n\geq N_2, \implies \absval{b_n} \leq C_2\absval{y_n}.$$
If $n\geq \max (N_1,N_2)$, then
 $\absval{a_nb_n} \leq C_1C_2\absval{x_ny_n} = C\absval{x_ny_n}$, with $C=C_1C_2$. This gives  $a_nb_n = \boo{x_ny_n}$.
\end{pf}

\begin{thm}[Lexicographic Order of Exponentials]\label{thm:lexico-exps-big-oh} Let $(a,
b)\in\BBR$, $a>1$, $b>1$, and consider the sequences
$\seq{a^n}{n=1}{+\infty}$ and $\seq{b^n}{n=1}{+\infty}$. Then $a^n
<< b^n \iff a \leq b$.
\end{thm}
\begin{pf}Recall that if $r\in\BBR$, then $r^n\to 0$ if $|r|<1$ and if $|r|>1$ then $\seq{r^n}{n=1}{+\infty}$ diverges.
Put now  $r=\dfrac{a}{b}$  and use Theorem
\ref{thm:sufficient-conds-for-big-oh}.
\end{pf}
\begin{exa}
$\dfrac{1}{2^n}<< 1 << 2^n << e^n << 3^n$.
\end{exa}
\begin{lem}\label{lem:exps-are-faster-than-powers-big-oh} Let $a\in\BBR$,
$a>1$, $k\in\BBN\setminus \{0\}$. Then $n^k << a^n$.
\end{lem}
\begin{pf}
Using L'H\^{o}pital's Rule $k$ times, $\lim _{\ngroes}
\dfrac{n^k}{a^n}=0$. Now apply Theorem
\ref{thm:sufficient-conds-for-big-oh}.
\end{pf}
\begin{thm}[``Exponentials are faster than powers'']\label{thm:exps-are-faster-than-powers-big-oh} Let $a\in\BBR$,
$a>1$, $\alpha\in\BBR$. Then $n^\alpha << a^n$.
\end{thm}
\begin{pf}Put $k=\max (1, \floor{\alpha}+1)$. Then by Theorem
\ref{thm:lexico-powers-big-oh}, $n^\alpha << n^k$. By Lemma
\ref{lem:exps-are-faster-than-powers-big-oh}, $n^k<<a^n$, and by the
Transitivity of Big Oh (Theorem \ref{thm:transitivity-big-oh}),
$n^\alpha << n^k << a^n$.
\end{pf}
\begin{exa}
$$ n^{100}<<e^n. $$
\end{exa}
\begin{thm}[``Logarithms are slower than powers'']\label{thm:logs-are-slower-than-powers-big-oh} Let
$(\alpha ,\beta) \in\BBR^2$, $\alpha >0$. Then $ (\log n)^{\beta}<<
n^\alpha$.
\end{thm}
\begin{pf}If $\beta \leq 0$, then $(\log n)^\beta <<1$ and the assertion is evident, so assume $\beta >0$.
For $x>0$, then $\log x < x$. Putting $x=n^{\alpha /\beta}$, we get
$$ \log n^{\alpha /\beta} < n^{\alpha/\beta}  \implies \log n < \dfrac{\beta n^{\alpha/ \beta}}{\alpha}
\implies (\log n)^\beta < \dfrac{\beta^\beta n^\alpha}{\alpha^\beta
},$$ whence $(\log n)^\beta << n^\alpha$.
\end{pf}

By the Multiplication Rule (Theorem \ref{thm:multiply-big-oh}) and
Theorems \ref{thm:lexico-powers-big-oh},
\ref{thm:exps-are-faster-than-powers-big-oh},
\ref{thm:logs-are-slower-than-powers-big-oh}, in order to compare
two expressions of the type $a^nn^b(\log )^c$ and $u^nn^{v}(\log
)^{w}$ we simply look at the lexicographic order of the exponents,
keeping in mind that logarithms are slower than powers, which are
slower than exponentials.

\begin{exa}
In increasing order of growth we have
$$\dfrac{1}{e^n}  << {\dfrac{1}{2^n}} <<{\dfrac{1}{n^2}} =
{\dfrac{1}{\log n}} << {1} <<{(\log\log n)^{10}} <<{\sqrt{\log n}}
<<{\dfrac{n}{\log n}}<< {n} <<{n\log n} <<{e^n}.
$$
\end{exa}
\begin{exa}
Decide which one grows faster as $\ngroes$: $n^{\log n}$ or $(\log
n)^n$.
\end{exa}
\begin{solu}
Since $n^{\log n} = e^{(\log n)^2}$ and $(\log n)^n=e^{n\log\log
n}$, and since $(\log n)^2<<n\log\log n$, we conclude that $n^{\log
n}<<(\log n)^n$.
\end{solu}

\bigskip

We now define two more fairly common symbols in asymptotic analysis.
\begin{df}
We write $a_n = \soo{b_n}$ if $\dfrac{a_n}{b_n}\rightarrow 0$ as
$n\rightarrow +\infty$, and say that $a_n$ is {\em small oh} of
$b_n$, or that $a_n$ {\em grows slower} than $b_n$ as $\ngroes$.
\end{df}
\begin{df}
A sequence $\seq{a_n}{n=1}{+\infty}$ is said to be {\em
infinitesimal} if $a_n=\soo{1}$, that is, if $a_n\rightarrow 0$ as
$\ngroes$.
\end{df}
\begin{rem}
We know from above that for $a>1$ $\lim _{\ngroes}
\dfrac{n^\alpha}{a^n} = 0$, and so $n^\alpha = \soo{a^n}$. Also, for
$\gamma >0$, $\lim _{\ngroes} \dfrac{(\log n)^\beta}{n^\gamma} = 0$,
and so $(\log n)^\beta = \soo{n^\gamma}$.
\end{rem}

\begin{df} We write $a_n \sim b_n$ if $\dfrac{a_n}{b_n}\rightarrow 1$ as
$n\rightarrow +\infty$, and say that $a_n$ is {\em asymptotic} to
$b_n$.
\end{df}
Asymptotic sequences are thus those that grow at the same rate as
the index increases. \vspace{2cm}
\begin{figure}[h]
$$\psset{unit=1pc} \pscircle(-2,0){4}\pscircle(2,0){4}
\pscircle(0,0){1}\rput(0,0){f\sim g}
\pscircle(-3.8,-1){1.7}\rput(-3.8,-1){f=\soo{g}}
\pscircle(3.8,-1){1.7}\rput(3.8,-1){g=\soo{f}}
\rput(-3.3,2.5){f=\boo{g}}\rput(3.3,2.5){g=\boo{f}}
$$\vspace{1cm}\footnotesize\hangcaption{Diagram of $O$ relations.} \label{fig:O-relations}
\end{figure}

\begin{exa}
The sequences $\seq{n^2-n\sin n}{n=1}{+\infty}$,
 $\seq{n^2 + n-1}{n=1}{+\infty}$ are asymptotic since
 $$ \dfrac{n^2-n\sin n}{n^2+n-1} = \dfrac{1-\dfrac{\sin n}{n}}{1+\dfrac{1}{n}-\dfrac{1}{n^2}}\rightarrow 1,
 $$as $\ngrows$.
\end{exa}
\begin{thm}Let $\seq{a_n}{n=1}{+\infty}$ and
$\seq{b_n}{n=1}{+\infty}$ be two properly diverging sequences. Then
$$a_n\sim b_n \iff a_n = b_n(1+\soo{1}).$$
\end{thm}
\begin{pf}Since the limit is $1>0$, either both diverge to $+\infty$ or both to
$-\infty$. Assume the former, and so, eventually, $b_n$ will be
strictly positive. Now,
$$ \begin{array}{lll}\lim _{\ngroes} \dfrac{a_n}{b_n} = 1 & \iff &  \forall \varepsilon >0, \exists N>0, 1-\varepsilon<\dfrac{a_n}{b_n}<
1+\varepsilon\\
&  \iff &  b_n - b_n\varepsilon < a_n < b_n + b_n\varepsilon\\
&  \iff &  \absval{a_n-b_n}< b_n\varepsilon\\
&  \iff & a_n-b_n = \soo{b_n}.
\end{array}
$$
\end{pf}


The relationship between the three symbols is displayed in figure
\ref{fig:O-relations}.





\section*{\psframebox{Homework}}
\addcontentsline{toc}{subsection}{Homework}
\begin{multicols}{2}\columnseprule 1pt \columnsep
25pt\multicoltolerance=900

\begin{exer}Prove that $\boo{\boo{a_n}}=\boo{a_n}$.
\end{exer}

\begin{exer} Let $k\in\BBR$ be a constant. Prove that $k+\boo{a_n} = \boo{k+a_n} = \boo{a_n}$.

\end{exer}
\begin{exer} Let $k\in\BBR$, $k>0$, be a constant. Prove that $(a_n+b_k)^k <<a_n ^k + b_n ^k$.

\end{exer}
\begin{exer}
For a sequence of real numbers $\seq{a_n}{n=1}{+\infty}$ it is known
that $a_n = \boo{n^2}$ and $a_n=\soo{n^2}$. Which of the two
statements conveys more information?
\begin{answerexer}
$a_n=\soo{n^2}$ does, since this says that $\lim
_{\ngroes}\dfrac{a_n}{n^2}=0$, whereas $a_n=\boo{n^2}$ says that
$\dfrac{a_n}{n^2}$ is bounded by some positive constant.
\end{answerexer}
\end{exer}
\begin{exer}
True or false: $a_n = \boo{n} \implies a_n= \soo{n}$.
\begin{answerexer}
False. Take $a_n=2n$, for example. Then $a_n<<n$,
$\dfrac{a_n}{n}=2$, and so $\dfrac{a_n}{n}\nrightarrow 0$.
\end{answerexer}
\end{exer}
\begin{exer}
True or false: $a_n = \soo{n} \implies a_n= \boo{n}$.
\begin{answerexer}
True. $\dfrac{a_n}{n}\rightarrow 0$ and so by Theorem
\ref{thm:sufficient-conds-for-big-oh}, $a_n<<n$.
\end{answerexer}
\end{exer}
\begin{exer}
True or false: $a_n = \soo{n^2} \implies a_n= \boo{n}$.
\begin{answerexer}
False. Take $a_n =n^{3/2}$. Then $\dfrac{a_n}{n^2}\rightarrow 0$ but
$a_n\neq \boo{n}$.
\end{answerexer}
\end{exer}
\begin{exer}
True or false: $a_n = \soo{n} \implies a_n= \boo{n^2}$.
\begin{answerexer}
True. $\dfrac{a_n}{n}\rightarrow 0$ and so by Theorem
\ref{thm:sufficient-conds-for-big-oh}, $a_n<<n$. Since $n<<n^2$, the
assertion follows by transitivity.
\end{answerexer}
\end{exer}

\end{multicols}


\section{Some Asymptotic Estimates}

We are mainly interested in providing asymptotic estimates for sums. In the case when a closed formula for the sum is known, the problem is half solved.
If the terms of a sum are monotonic, then one may apply a method akin to the integral test.
\begin{exa}
Since $1+2+\cdots + n =\dfrac{n^2}{2}+\dfrac{n}{2}$, we have   $1+2+\cdots + n \sim \dfrac{n^2}{2}$.
\end{exa}

\begin{exa}[Harmonic Sum]\label{exa:estima-harmonic}
Prove that $1+\dfrac{1}{2}+\dfrac{1}{3}+\cdots + \dfrac{1}{n}\sim \log n$.
\end{exa}
\begin{solu}
Using the fact that $x\mapsto \dfrac{1}{x}$ is decreasing for $x>0$, if $k >0$ is an integer, for $x\in ]k;k+1[$,
$$ \dfrac{1}{k+1}< \dfrac{1}{x}<\dfrac{1}{k}\implies \int _k ^{k+1}\dfrac{\d{x}}{k+1}< \int _k ^{k+1}\dfrac{\d{x}}{x}<\int _k ^{k+1}\dfrac{\d{x}}{k} \implies
\dfrac{1}{k+1}< \int _k ^{k+1}\dfrac{\d{x}}{x}<\dfrac{1}{k}. $$
Letting $k$ run from $1$ to $n-1$ on the first inequality we deduce,
$$ \dfrac{1}{2}+\dfrac{1}{3}+\cdots + \dfrac{1}{n} < \int _1 ^n \dfrac{\d{x}}{x} \implies
1+\dfrac{1}{2}+\dfrac{1}{3}+\cdots + \dfrac{1}{n} < 1+\int _1 ^n \dfrac{\d{x}}{x} = 1+\log n \implies \dfrac{1+\dfrac{1}{2}+\dfrac{1}{3}+\cdots + \dfrac{1}{n}}{\log n} < 1+\dfrac{1}{\log n}. $$
Letting $k$ run from $1$ to $n-1$ on the second inequality,
$$ \int _1 ^n \dfrac{\d{x}}{x} < 1+\dfrac{1}{2}+\dfrac{1}{3}+\cdots + \dfrac{1}{n-1} \implies \log n+\dfrac{1}{n}< 1+\dfrac{1}{2}+\dfrac{1}{3}+\cdots + \dfrac{1}{n} \implies
1+\dfrac{1}{n\log n}<\dfrac{1+\dfrac{1}{2}+\dfrac{1}{3}+\cdots + \dfrac{1}{n}}{\log n}. $$The assertion now follows by the Sandwich Theorem.
\end{solu}
\begin{exa}\label{exa:estima-sum-logs}
Using Calculus it can be proved that $x\mapsto x\log x$ is increasing for $x>e^{-1}$. Recall that using an
integration by parts, $\int _1 ^n \log x\ \d{x}=n\log n-n+1$. Use this to find an asymptotic estimate for $\sum _{k=1} ^n \log k$.
\end{exa}
\begin{solu} We use the same method as in example \ref{exa:estima-harmonic}.
If $k >0$ is an integer, for $x\in ]k;k+1[$,
$$ \log k < \log x <\log (k+1)\implies \int _k ^{k+1}\log k\d{x}< \int _k ^{k+1}\log x\d{x}<\int _k ^{k+1}\log (k+1)\d{x} \implies
\log k< \int _k ^{k+1}\log x\d{x}<\log (k+1). $$
Letting $k$ run from $1$ to $n-1$ on the first inequality we deduce,
$$\begin{array}{lll} \log 1 + \log 2 + \cdots + \log (n-1)< \int _1 ^n \log x  & \implies &
\log 1 + \log 2 + \cdots + \log (n-1) <  \int _1 ^n \log x \d{x} =   n\log n-n+1\\ &  \implies &  \log 1 + \log 2 + \cdots + \log n  <\log n + n\log n-n+1.
\end{array}$$
Letting $k$ run from $1$ to $n-1$ on the second inequality,
$$\begin{array}{lll} \int _1 ^n  \log x \d{x} < \log 2 + \log 3 + \cdots + \log n  & \implies &  n\log n< \log 2 + \log 3 + \cdots + \log n \\
& \implies &
n\log n-n+1=n\log n-n+1+\log 1<\log 1+  \log 2 + \log 3 + \cdots + \log n. \end{array} $$
We deduce that
$$  1-\dfrac{1}{\log n}+\dfrac{1}{n\log n}<\dfrac{\log 1 + \log 2 + \cdots + \log n}{n\log n}< 1+\dfrac{1}{n}-\dfrac{1}{\log n}+\dfrac{1}{n\log n}.$$
The Sandwich Theorem now gives $\sum _{1\leq k \leq n} \log k \sim n\log n$.
\end{solu}
\begin{exa}
Prove that for sufficiently large $n$,  $$e\dfrac{n^n}{e^n}<n!< e\dfrac{n^{n+1}}{e^n}.$$
\end{exa}

\begin{solu}
From
 example \ref{exa:estima-sum-logs},
$$n\log n -n + 1 <\log n!< n\log n - n + \log n+1, $$
which gives upon exponentiation,
$$ e\dfrac{n^n}{e^n}<n! < e\dfrac{n^{n+1}}{e^n}. $$
\end{solu}

\begin{rem}
The true order of magnitude of $n!$ is given by Stirling's formula:
$$n! \sim \dfrac{n^n}{e^n}\sqrt{2\pi n}.  $$
\end{rem}




\section*{\psframebox{Homework}}
\addcontentsline{toc}{subsection}{Homework}
\begin{multicols}{2}\columnseprule 1pt \columnsep
25pt\multicoltolerance=900

\begin{exer}
Let $f_n$ denote the $n$th Fibonacci number. Shew that $f_n = \boo{1.62^n}$.
\end{exer}

\begin{exer}
Prove that $e^n<<n!$.
\begin{answerexer}
For $n \geq 3$,
$$ \underbrace{e\cdot e \cdots e}_{n\ \mathrm{times}} \leq e \cdot e 3\cdot 4 \cdots n = \dfrac{e^2n!}{2} \implies e^n=\boo{n!}.  $$
\end{answerexer}
\end{exer}


\begin{exer}
Prove that $1+\dfrac{1}{\sqrt{2}}+\dfrac{1}{\sqrt{3}}+\cdots  +\dfrac{1}{\sqrt{n}} \sim 2\sqrt{n}$.
\begin{answerexer}
Use the fact that $x\mapsto \dfrac{1}{\sqrt{x}}$ decreases for $x>0$.
Then $$ \dfrac{1}{\sqrt{k+1}} <\int _k ^{k+1}\dfrac{\d{x}}{\sqrt{x}} < \dfrac{1}{\sqrt{k}}$$
gives
$$ \sum _{k=2} ^{n}\dfrac{1}{\sqrt{k}}< \int _1 ^n \dfrac{\d{x}}{\sqrt{x}} < \sum _{k=1} ^{n-1} \dfrac{1}{\sqrt{k}}, $$
which implies that $$  2\sqrt{n}-2+\dfrac{1}{\sqrt{n}} <\sum _{k=1} ^{n} \dfrac{1}{\sqrt{k}}< 2\sqrt{n}-1, $$from where the required result is
easily deduced.
\end{answerexer}
\end{exer}

\begin{exer}
From the fact that $x\mapsto \log x$ is a concave function, deduce that
$$x\in]k;k+1[ \implies  \log k + \log (k+1) < 2\log x.$$ Use this to improve the upper bound in example \ref{exa:estima-sum-logs}.
\end{exer}


\end{multicols}

\section{Analysis of Algorithms}

In this section we will provide rough estimates for the {\em time} that takes out to carry out some algorithms. The problem at hand
is the following: given an input of
size $n$ (however that ``size'' is measured), which we will assume grows indefinitely towards $+\infty$, we would like to know how the memory requirements and the running time for a computer to process it, in
fact, we would like to find a certain function $f$
and say that the algorithms {\em complexity}  is
$\boo{f(n)}$.
\begin{exa}
Suppose it takes a digital camera   $10^{-6}$ of a second to process a pixel. Estimate how much time will it take it to handle a
$1$ megapixel (that is, one million pixels) image if the algorithm it uses is of complexity $\boo{n}$, $\boo{n\log n}$, or
$\boo{n^2}$, where $n$ is the number of pixels.
\end{exa}
\begin{solu}
In the case of the linear algorithm, the camera takes about $10^{-6}10^6 = 1$ second. If the algorithm is of order $n\log n$, the camera takes about
$10^{-6} (10^6)\log 10^6 \approx 13$ seconds. If the algorithm is of complexity $n^2$, the camera will take about $10^{-6}10^{12} = 10^6$ seconds. Now, a week is
$$ 7 \times 24 \times 60\times 60 = 604800 $$seconds, so it would take the camera approximately about $11$ days to process such a picture!
\end{solu}


\begin{df}A {\em bit} is a binary unit: either a $0$ or a $1$. The {\em bit complexity} of an algorithm is the number of steps
that it takes to process a given input measured in bits.
\end{df}

\begin{exa}[Bit Complexity of Ordinary Addition] Two positive integers $m$ and $n$ are to be added. Find the order of bit operations required to carry out their sum.
\end{exa}
\begin{solu}Assume without loss of generality that
$m\leq n$. First we convert $m$ and $n$ into bits: $m$ has $\floor{\log _2 m}+1$ bits and $n$ has $\floor{\log _2 n}+1$. We line up the bits and
add them. There are at most  $\floor{\log _2 n}+1$ sums performed and  at most $\floor{\log _2 m}+1$ carries. Hence, there are about
$\boo{\floor{\log _2 n}+1+ \floor{\log _2 m}+1}  = \boo{\log n}$ bit operations.
\end{solu}


\begin{exa}[Bit Complexity of Ordinary Multiplication]  Two positive integers $m$ and $n$ are to be multiplied. Find the order of bit operations required to carry
out their product.
\end{exa}
\begin{solu}Assume without loss of generality that
$m\leq n$. Again, we first we convert $m$ and $n$ into bits: $m$ has $\floor{\log _2 m}+1$ bits and $n$ has $\floor{\log _2 n}+1$.
We multiply bit by bit requiring $(\floor{\log _2 n}+1)(\floor{\log _2 m}+1) = \boo{(\log n)^2}$ partial multiplications. After the partial
multiplications we need at most $\boo{\floor{\log _2 n}+1+\floor{\log _2 m}+1}=\boo{\log n}$ additions of at most $\boo{\floor{\log _2 n}+1}$ bits, that is,
$\boo{(\log n)^2}$ additions. Hence, ordinary multiplication requires
$\boo{(\log n)^2+(\log n)^2} = \boo{\log^2 n}$ bit operations.
\end{solu}




Most algorithms that take just a \verb+for+ loop are easy to analyse: the number of operations they take to perform  is about the length of the loop.
Thus if we have a for loop of the form
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{S1; for k from 1 to n do S2; od;}{}
\end{mapleinput}
\end{maplegroup}
then this fraction of the algorithm has computational time $t_1+nt_2$ where $t_1$ and $t_2$ are, respectively, the computational times of the statements
 $S1$ and $S2$.

\bigskip

The test in a conditional statement  has usually a bit complexity of $\boo{1}$, which must be added to its branchings \verb+then+ or \verb+else+.

\begin{exa}
Let $K$ be a constant. Find the bit complexity of the fragment
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{for k from 1 to K do O(1) od;}{}
\end{mapleinput}
\end{maplegroup}
\end{exa}
\begin{solu}
In this case the complexity of the fragment is $K\boo{1} = \boo{K}=\boo{1}$, since $K$ is a constant.
\end{solu}
\begin{exa}
Find the bit complexity of the fragment
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{for k from 1 to n do O(1) od;}{}
\end{mapleinput}
\end{maplegroup}
\end{exa}
\begin{solu}
In this case the complexity of the fragment is $n\boo{1} = \boo{n}$.
\end{solu}
\begin{exa}
Find the bit complexity of the imbricated loop
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{for k from 1 to n do for j from 1 to n do  O(1) od;  od;}{}
\end{mapleinput}
\end{maplegroup}
\end{exa}
\begin{solu}
The inner \verb+for+ loop has complexity $n\boo{1} = \boo{n}$. The outer \verb+for+ loop is simply adding these complexities, and hence the
fragment  has
complexity $n\boo{n} = \boo{n^2}$. Alternatively, there are $\sum _{1 \leq k \leq n}\ \sum _{1 \leq j \leq n}\boo{1} = n^2\boo{1} = \boo{n^2}$ bit operations.
\end{solu}
\begin{exa}
Find the bit complexity of the imbricated loop
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{for k from 1 to n do for j from 1 to i do  O(1) od;  od;}{}
\end{mapleinput}
\end{maplegroup}
\end{exa}
\begin{solu}There are $\sum _{1 \leq k \leq n}\ \sum _{1 \leq j \leq i}\boo{1} = \sum _{1 \leq k \leq n} i\boo{1} = \dfrac{n(n+1)}{2}\boo{1}=\boo{n^2}$ operations.
\end{solu}
\begin{exa}
Find the bit complexity of the fragment
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{c:=1; while (c<n) do  O(1); c:=2*c; od; }{}
\end{mapleinput}
\end{maplegroup}
\end{exa}
\begin{solu} After $i$ iterations, the value of $c$ will be $2^i$. We need $2^i < n \implies i< \log _2 n$. Thus the number of iterations and the
complexity of the loop is  $\boo{\log _2 n} = \boo{\log n}$.
\end{solu}
\begin{exa}
Find the bit complexity of the fragment
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{c:=n; while (c>1) do  O(1); c:=c/2; od; }{}
\end{mapleinput}
\end{maplegroup}
\end{exa}
\begin{solu} After $i$ iterations, the value of $c$ will be $\dfrac{n}{2^i}$. We need $\dfrac{n}{2^i} > 1 \implies i< \log _2 n$. Thus the number of iterations and the
complexity of the loop is  $\boo{\log _2 n} = \boo{\log n}$.
\end{solu}


Sometimes we are simply interested in the {\em number of operations} (additions, multiplications, etc.) necessary to carry out a task. In such cases,
we over-estimate by looking at the worst case scenario.


\begin{exa}
What is the worst-case running time of the following program?
\begin{maplegroup}
\begin{mapleinput}
\mapleinline{active}{1d}{\maplemultiline{> a:=proc(n) \\ x:=0; \\ for\ i\ from\ 1\ to\ n-1\ do\\ for\ j\ from\ i+1\ to\ n\ do\\ for\ k\ from\ 1\ to\ do\\ x:=x+1;\ od;\ od;\ od;\\ RETURN(x);\\ end;  }}{}
\end{mapleinput}
\end{maplegroup}
\end{exa}
\begin{solu}
Each of the  \verb+for+ loop takes about $\boo{n}$ operations, hence the worst running time is about $\boo{n^3}$.
\end{solu}
\begin{exa}[Eratosthenes Sieve] Calculate the number of operations of Eratosthenes sieve of example \ref{exa:eratos}.
\end{exa}
\begin{solu}For given $n>0$
Observe that we loop over $\sqrt{n}$ potential divisors. For each divisor $k$, we cross out $\dfrac{n}{k}$ numbers.
The number of operations carried out is $$\sum _{1\leq k \leq \sqrt{n}} \dfrac{n}{k} = n\sum _{1\leq k \leq \sqrt{n}} \dfrac{1}{k}\sim n\log \sqrt{n} =\boo{n\log n}, $$
where we have used the result of example \ref{exa:estima-harmonic}.
\end{solu}



\section*{\psframebox{Homework}}
\addcontentsline{toc}{subsection}{Homework}
\begin{multicols}{2}\columnseprule 1pt \columnsep
25pt\multicoltolerance=900
\begin{exer}
What is the complexity of the algorithm for finding the maximum of a list of example \ref{exa:maxi-list}?
\begin{answerexer}
$\boo{n}$, where $n$ is the size of the list.
\end{answerexer}
\end{exer}
\begin{exer}
What is the complexity of the algorithm for finding the linear search in an unsorted dictionary of example \ref{exa:lin-search}?
\begin{answerexer}
$\boo{n}$, where $n$ is the size of the dictionary.
\end{answerexer}
\end{exer}

\begin{exer}
What is the complexity of the algorithm for finding the $n$th power of $x$ of example \ref{exa:powers2}?
\begin{answerexer}
$\boo{\log n}$.
\end{answerexer}
\end{exer}
\begin{exer}
What is the worst case complexity of the bubblesort algorithm of example \ref{exa:bubble}?
\begin{answerexer}
$\boo{n^2}$.
\end{answerexer}
\end{exer}
\begin{exer}
What is the worst case complexity of the quicksort algorithm of example \ref{exa:quickie}?
\begin{answerexer}
$\boo{n^2}$.
\end{answerexer}
\end{exer}


\end{multicols}







\Closesolutionfile{discmatII}
\chapter{Answers and Hints}\addcontentsline{toc}{section}{Answers and
Hints}\markright{Answers and Hints}
 {\small \input{discmatII1}}

\begin{thebibliography}{mmm.m}

\bibitem[Boo]{Boo} George BOOLE, {\em A Treatise on the Calculus of Finite Differences}.
\bibitem[BoMu]{BoMu} J.A. BONDY and U.S.R. MURTY, {\em Graph Theory with Applications}.

\bibitem[CLRS]{CLRS} Thomas CORMEN, Charles LEISERSON, Ronald RIVEST, Clifford STEIN, {\em Introduction to Algorithms}.
\bibitem[Chr]{Chr} George CHRYSTAL, {\em Textbook of Algebra}.
\bibitem[DeB]{DeB} N.G. de Bruijn, {\em  Asymptotic Methods in Analysis }.
\bibitem[Dij]{Dij} Edsger W. DIJKSTRA, {\em  A discipline of programming}, Prentice Hall, 1976.
\bibitem[Gri]{Gri} David GRIES, {\em The Science of Programming}. Springer, 1981.
\bibitem[HaKn]{HaKn} H.S. HALL, and S.R. KNIGHT, {\em Algebra for Beginners}, Macmillan, 1st ed., 1893.
\bibitem[HaRi]{HaRi} Nora HARSTFIELD and Gerhard RINGEL, {\em Pearls in Graph Theory: A Comprehensive Introduction}.
\bibitem[HeKa]{HeKa} Israel HERSTEIN and Irving KAPLANSKY, {\em Matters Mathematical}.
\bibitem[Knu]{Knu} Donald KNUTH, {\em The Art of Computer Programming}, Vols I, II and III.
\bibitem[KGP]{KGP} Donald KNUTH, Ronald GRAHAM, and Oren PATASHNIK, {\em Concrete Mathematics}
\bibitem[MoVa]{MoVa} H.L. MONTGOMERY and R.C. VAUGHAN, {\em Multiplicative Number Theory I : Classical Theory}.
\bibitem[Ser]{Ser} Raymond SEROUL, {\em Programming for Mathematicians}, Springer Verlag, 2000.
\bibitem[She]{She} Alexander SHEN, {\em  Algorithms and Programming: Problems and Solutions}, Birkhauser, 1997.
\bibitem[Wil]{Wil} Herbert WILF, {\em Generatingfunctionology}.
\bibitem[WM]{WM} {\bf Waterloo Maple}, {\em Maple V Programming Guide}.


\end{thebibliography}






\end{document}

\end{document}
